DISTINCT in SQL
By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17
Before this SELECT and Column Expressions, FROM and Table References
Builds toward DISTINCT ON
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 customersTen 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.
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
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
customerstable contains every customer Brightlane has on file. - Many customers share the same country, so the raw
countrycolumn has heavy duplication.
Output:
- One row per distinct country, with a single column
country.
Schema · ecommerce5 tables? = nullable
Run previews · Check grades
Write a query, then run it to see results here.
The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.
See the full worked solutionStation Zero, our free browser SQL game, teaches this concept inside a story. No signup.
count the survivors in Station Zero9 DISTINCT practice problems
Write a query to return each country represented in the customer base, with no duplicates.
Write a query to return each status value that appears in the orders table, with no duplicates.
Write a query to return each office location represented in the departments data, with no duplicates.
Write a query to return each customer ID that appears in the orders data, with no duplicates.
Write a query to return each unique job title held by an employee, with no duplicates.
Write a query to return each unique pairing of country and active status from the customer base.
Write a query to return each unique pairing of plan and country from the users table.
Write a query to return one row per unique city value.
Write a query to return each unique stock-value level, with no duplicates.
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.