SQL DISTINCT
SELECT DISTINCT removes duplicate rows from a result. Use it to list the unique values in a column, like every city your customers live in.
Updated December 1, 2026
Syntax
SELECT DISTINCT column1, column2
FROM table_name;
List unique values
The drinks table has 10 rows but only three categories.
SELECT DISTINCT category
FROM drinks;
| category |
|---|
| coffee |
| tea |
| other |
DISTINCT and NULL
DISTINCT treats all NULL values as one value, so a missing city appears once in the list.
SELECT DISTINCT city
FROM customers;
| city |
|---|
| Austin |
| Seattle |
| Denver |
| NULL |
Count unique values
COUNT(DISTINCT column) counts different non-null values. Fay has no city, so she isn’t counted.
SELECT COUNT(*) AS customers, COUNT(DISTINCT city) AS cities
FROM customers;
| customers | cities |
|---|---|
| 8 | 3 |
Common mistakes
DISTINCTapplies to the whole row, not just the first column.SELECT DISTINCT category, namereturns every drink.- Using
DISTINCTto hide duplicate rows caused by a wrong JOIN. Fix the join instead. - Expecting
DISTINCTto sort. AddORDER BYif order matters.
Practice DISTINCT interactively
Write real queries in your browser and get instant feedback in the lesson “Rename and dedupe” (Unit 1). New learners can start free from the first lesson.
Frequently asked questions
What is the difference between DISTINCT and GROUP BY?
For listing unique values they give the same rows. Use GROUP BY when you also want to count or total something for each value.
Is DISTINCT slow?
It has to compare rows to find duplicates, so it adds work on large results. That’s usually fine, but don’t add it “just in case”.