Tier 1 · Foundations

DISTINCT in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

What is DISTINCT in SQL?

DISTINCT removes duplicate rows from a query's output.

SQL returns every matching row by default, duplicates included. If 1,000 customers all live in the US and you select country, you get 1,000 rows all saying "US." That's not useful when you want a list of unique values.

DISTINCT collapses duplicates so each distinct value appears exactly once. It answers a question analysts ask constantly: what unique values actually exist in this column?

How do you list the unique values in a column?

You're exploring a table you haven't worked with before. You want to know what statuses exist in the orders table, what countries are represented in the customers table, what product categories are in the catalog. DISTINCT turns a column full of repeated values into a clean enumeration:

SELECT DISTINCT country
FROM customers

Ten thousand customers, 40 countries: you get 40 rows back. One per unique value.

Does DISTINCT apply to one column or the whole row?

DISTINCT operates on the entire row, not just one column. When you select multiple columns, it returns one row per unique combination:

SELECT DISTINCT country, is_active
FROM customers

A country where some customers are active and others aren't shows up twice — once for the active combination, once for the inactive. Those two are different rows, so both survive deduplication.

Why does DISTINCT return more rows when you add a column?

The one thing that trips people up: adding a column to the SELECT list almost always increases the row count.

SELECT DISTINCT country returns 40 rows. SELECT DISTINCT country, city returns many more — one row per unique country/city pair. The deduplication scope widens every time you add a column. DISTINCT doesn't deduplicate on one column while ignoring the others. It deduplicates on everything you selected.

Check your understanding

You run SELECT DISTINCT country FROM customers and get 40 rows. Then you change it to SELECT DISTINCT country, city. How many rows do you expect?

Practice DISTINCT in SQL

Practice · easy ecommerce · Brightlane

Brightlane's growth team is building a regional marketing strategy and needs to identify which countries have registered customers.

Write a query to return each country represented in the customer base, with no duplicates.

Assumptions:

  • The customers table contains every customer Brightlane has on file.
  • Many customers share the same country, so the raw country column has heavy duplication.

Output:

  • One row per distinct country, with a single column country.
Schema · ecommerce5 tables? = nullable
categories
idinteger
nametext
parent_id?integer
products
idinteger
nametext
category_id?integer
pricenumeric
stock_qtyinteger
attributes?jsonb
order_items
idinteger
order_id?integer
product_id?integer
quantityinteger
unit_pricenumeric
customers
idinteger
nametext
emailtext
city?text
countrytext
created_attimestamptz
is_activeboolean
orders
idinteger
customer_id?integer
ordered_attimestamptz
statustext
total_amountnumeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution
In the game

Station Zero, our free browser SQL game, teaches this concept inside a story. No signup.

count the survivors in Station Zero

9 DISTINCT practice problems

Start learning to practice all 9 DISTINCT problems, with instant grading and mastery tracking.

Common questions about DISTINCT

Is SELECT DISTINCT the same as GROUP BY?

For a plain list of unique values, they return the same rows. GROUP BY earns its keep when you also want an aggregate for each group; DISTINCT says only that you want each combination once. Use whichever states your intent, not whichever you typed first.

How does DISTINCT treat NULL?

It treats every NULL as the same value, so a column with many missing entries contributes exactly one NULL row to the result. That is the opposite of how NULL behaves in a comparison, where it never equals anything, itself included.

Does DISTINCT sort the rows?

Not as a promise. PostgreSQL may deduplicate by sorting, so the output can look ordered, but nothing guarantees it will stay that way as the data or the plan changes. Add ORDER BY when the order matters.

Does DISTINCT slow a query down?

It does more work than leaving it off, because the database has to compare rows to find the duplicates. The honest answer for any given query is to read its plan rather than to guess, and to check first whether the duplicates are real or a join is manufacturing them.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.