# DISTINCT

> Remove duplicate rows from a result with DISTINCT, and see how NULLs and several columns change the answer.

Subject: SQL | Lesson 10 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/sql/distinct-values
Previous: [ORDER BY, LIMIT & OFFSET](https://codewithanimation.in/sql/order-by-limit-offset.md) | Next: [Expressions & Functions](https://codewithanimation.in/sql/expressions-and-functions.md)

## DISTINCT keeps one of each

`SELECT DISTINCT` removes duplicate rows from the result. Two rows are duplicates when every selected value is the same.

The opposite is `ALL`, which keeps every row. `ALL` is the default, so you only write `DISTINCT` when you want rows removed.

For this comparison only, two `NULL` values count as equal. Everywhere else in SQL they do not.

## Answer "which values exist?"

Questions like "which teams do we have?" or "which cities do our customers live in?" want each value once. `DISTINCT` gives you that list without counting or grouping.

## DISTINCT works on the whole row

Eight players belong to three teams. This query lists each team once:

```sql
SELECT DISTINCT team
FROM players
ORDER BY team;
```

Select two columns and `DISTINCT` compares the pair: `SELECT DISTINCT team, points` keeps one row for each different team and points combination. Ben and Hana are both Blue with 17 points, so they make one row. Chen and Eli both have NULL points, but they are on different teams, so their rows stay separate.

PostgreSQL also has `DISTINCT ON (column)`, which keeps the first row of each group. It is an extension, not standard SQL, and "first" is unpredictable unless an `ORDER BY` decides it.

## Common mistake: DISTINCT can hide a mistake

If a query returns duplicates you did not expect, adding `DISTINCT` makes them disappear but does not explain them. The cause is often a join that repeats rows, which you will meet soon. Fix the cause, not the symptom.

Remember that `DISTINCT` looks at all selected columns. It is not "distinct on the first column".

## Tip: Add ORDER BY to a DISTINCT list

A distinct list comes back in no particular order. If people will read it, add `ORDER BY` so it is the same every time.

## Try it: Watch duplicates collapse

Pick the columns and switch between `DISTINCT` and all rows. Compare how many rows you get.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/distinct-values

## Check yourself

### Question 1

`SELECT DISTINCT team FROM players` runs on 8 players in 3 teams. How many rows come back?

- A. 3
- B. 8
- C. 1

Correct answer: A

One row for Red, one for Green and one for Blue.

- Why not B: That is every row. DISTINCT removes the duplicate teams.
- Why not C: DISTINCT keeps one row for each different value, not one row in total.

### Question 2

Two players have NULL points. How many NULL rows does `SELECT DISTINCT points FROM players` return?

- A. One
- B. Two
- C. None, because NULLs are removed

Correct answer: A

DISTINCT treats NULLs as equal to each other, so they become a single row.

- Why not B: For DISTINCT, NULL values are treated as equal, so they collapse into one.
- Why not C: DISTINCT never removes NULL. It keeps one NULL row.

### Question 3

`SELECT DISTINCT team, points FROM players` runs on 8 players. Ben and Hana are both Blue with 17 points. How many rows come back?

- A. 7: Ben and Hana make one row
- B. 8: every player is different
- C. 3: one per team

Correct answer: A

DISTINCT compares the whole selected row. Only the Ben and Hana rows are identical.

- Why not B: Ben and Hana have identical team and points, so their rows are duplicates.
- Why not C: That would be DISTINCT on team alone. With two columns the pair is compared.

## Remember this

- DISTINCT removes rows whose selected values are all the same, and ALL (keep everything) is the default.
- For DISTINCT, NULL values count as equal, so they collapse into one row.
- With several columns, DISTINCT compares the combination of all of them.
- If DISTINCT is hiding unexpected duplicates, look for the cause, often a join.
