Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I remove duplicate result rows with SELECT DISTINCT?

Quick answer

In SQLite, put DISTINCT immediately after SELECT. It removes duplicate result rows based on all the columns you selected—not every column in the underlying table. Select only event_label to get one row per label; select event_label and venue to get one row per distinct label-and-venue combination. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have repeated event labels in an events table. How do I return each label just once?

Geez

Use DISTINCT after SELECT, and select only the column whose repeated values you want collapsed. For example, suppose this invented sample data is in a SQLite table named events:

id | event_label | venue
1  | Workshop    | Hall A
2  | Workshop    | Hall A
3  | Workshop    | Hall B
4  | Reading     | Hall A

To return one result row per distinct label, write:

SELECT DISTINCT event_label
FROM events;

The result is:

event_label
-----------
Workshop
Reading

The database compares the selected result rows when applying DISTINCT; here each result row contains only event_label. [S1] This removes duplicate rows from this query’s output. It does not delete or change any records in events.

Follow-up question

What if I also need to show each event’s venue?

Geez

Add venue to the selected columns, but note that this changes what counts as a duplicate. SQLite keeps a row for each distinct combination of selected values. So the two Workshop rows at Hall A collapse into one, while Workshop at Hall B remains separate. [S1]

SELECT DISTINCT event_label, venue
FROM events;

With the sample data above, the result is:

event_label | venue
------------|------
Workshop    | Hall A
Workshop    | Hall B
Reading     | Hall A

This query does not mean “one row per event label, with an arbitrary venue.” It means one row per unique event_label and venue pair. If the goal is one row per label, select only the label; if the goal is one row per label-and-venue pair, select both.

Follow-up question

How can I decide which columns belong in the SELECT list when I use DISTINCT?

Geez

Start by describing what one output row should represent. Then select the columns that define that row. For instance, “one row per label” maps to SELECT DISTINCT event_label; “one row per label at each venue” maps to SELECT DISTINCT event_label, venue.

Before adding a display column, ask whether two records with different values in that column should still count as the same output row. If yes, adding it to this SELECT DISTINCT can keep multiple rows where you expected one. If you need both one row per label and some additional information, clarify how that information should be chosen or summarized; DISTINCT alone does not specify a representative venue or a rule for selecting one.

Follow-up question

Can I use these examples as a template, and are there any edge cases to check?

Geez

Yes. Replace the example table and column names, then check that the selected columns match your definition of a duplicate. SQLite’s documented DISTINCT comparison rules treat two NULL values as equal; text comparison uses the applicable collation. [S1] The source explains SQLite behavior, not every SQL database, and it does not establish which query is appropriate for your data or guarantee how another database handles comparisons. Check your database’s documentation if you are using a different system.

Copyable pattern:

-- One output row per label
SELECT DISTINCT event_label
FROM events;

-- One output row per label-and-venue pair
SELECT DISTINCT event_label, venue
FROM events;

In the second query, every selected value participates in deciding whether a result row is a duplicate.

A practical checklist

  • Name what one output row should represent.

  • Select only the columns needed for that definition.

  • If selecting multiple columns, treat their combined values as the distinct key.

  • Compare the output of both query patterns on a small sample.

  • Confirm your database’s comparison behavior if you are not using SQLite.

A few more useful answers

Does SELECT DISTINCT delete duplicate records from my table?

No. It removes duplicate result rows from that query’s output; it does not delete or modify stored records.

Why do I still see the same label more than once?

Check whether you selected other columns too. Rows with the same label but different values in another selected column are distinct result rows.

Does this example apply to every SQL database?

The examples and cited comparison details are for SQLite. Check your database’s documentation before assuming its DISTINCT comparison rules are identical.

Sources

References used for this guide.

  1. [S1] SQLite SELECT: DISTINCT duplicate-row processing
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work