DISTINCT
Collapse duplicate rows into unique values
Beginner 8 minDISTINCTduplicatesunique
Removing Duplicates
DISTINCT removes duplicate rows from your result set. It compares all selected columns together as a unit — a row is only a duplicate if every single column value matches another row. Here's how it works:
Full-row comparison —
SELECT DISTINCT country gives unique countries. But SELECT DISTINCT country, city gives unique country-city pairs, so the same country can appear multiple times when paired with different city values.COUNT(DISTINCT column) — More efficient than fetching all distinct rows and counting them externally. Counts unique non-NULL values directly inside the database engine without returning all the rows to your application.
Performance cost — DISTINCT scans and compares every row in the result, which can be slow on large tables. Use it when you specifically need deduplication, not as a band-aid for poorly written queries.
Knowledge Canvas
How DISTINCT Works
Remove duplicate rows from results
- DISTINCT evaluates the entire row — all selected columns must match
- Applied AFTER SELECT, BEFORE ORDER BY
- Two NULLs are treated as equal by DISTINCT
- Affects ALL columns in the SELECT list
Performance Impact
DISTINCT isn't free
- DISTINCT requires sorting or hashing — can be slow on large sets
- Often a sign of a bad join — fix the join instead
- GROUP BY is sometimes more efficient than DISTINCT
- Index on the DISTINCT columns helps performance
Common Mistakes
DISTINCT traps
- DISTINCT on SELECT * rarely makes sense
- Adding more columns makes rows "more unique" — fewer duplicates removed
- DISTINCT in aggregate:
COUNT(DISTINCT col)≠COUNT(col) - Can't DISTINCT on one column while showing others easily
DISTINCT vs GROUP BY
Two ways to deduplicate
DISTINCT: simple dedup
GROUP BY: dedup + aggregate
SELECT DISTINCT country
SELECT country, COUNT(*)
No aggregation possible
SUM, COUNT, AVG available
Syntax
Syntax Template
1-- Unique row combinations
2SELECT DISTINCT
3 column1,
4 column2
5FROM table;
6
7-- Count unique non-NULL values
8SELECT
9 COUNT(DISTINCT column)
10FROM table;
DISTINCTPlaced after SELECT, before column namesCOUNT(DISTINCT col)Counts unique non-NULL values Sample Data
customers
12 rows
| id | name | country |
|---|---|---|
| C1001 | Aarav Sharma | India |
| C1002 | Sara Chen | Singapore |
| C1003 | James Wilson | UK |
| C1004 | Maria Garcia | Spain |
| C1005 | Yuki Tanaka | Japan |
| C1006 | Priya Patel | India |
| C1007 | Alex Johnson | USA |
| C1008 | Chen Wei | China |
| C1009 | Emma Brown | Australia |
| C1010 | Omar Hassan | UAE |
| C1011 | Lena Muller | Germany |
| C1012 | Ravi Kumar | India |
products
10 rows
| id | name | category | price | stock |
|---|---|---|---|---|
| P101 | Wireless Mouse | Electronics | 24.99 | 150 |
| P102 | Office Desk | Furniture | 189 | 25 |
| P103 | LED Desk Lamp | Furniture | 45.5 | 80 |
| P104 | USB-C Cable | Electronics | 12.99 | 500 |
| P105 | Bluetooth Speaker | Electronics | 79 | 60 |
| P106 | Office Chair | Furniture | 95 | 15 |
| P107 | Coffee Maker | Appliances | 89.99 | 40 |
| P108 | Notebook Set | Stationery | 8.5 | 200 |
| P109 | Wireless Headphones | Electronics | 159 | 30 |
| P110 | Standing Desk | Furniture | 425 | 8 |
Worked Example
List all unique countries from the customers table.
SQL
1SELECT DISTINCT
2 country
3FROM customers
4ORDER BY
5 country;
India appears 3 times in the data (Aarav, Priya, Ravi) but only once in the result. DISTINCT collapses all duplicates into a single row per unique value.
Output
10 rows
| country |
|---|
| Australia |
| China |
| Germany |
| India |
| Japan |
| Singapore |
| Spain |
| UAE |
| UK |
| USA |
Key Concepts
1DISTINCT compares the full row — all selected columns together
2SELECT DISTINCT country, city allows country repeats with different cities
3COUNT(DISTINCT col) is more efficient than DISTINCT + external count
4DISTINCT can be slow on large tables — use sparingly
Pro Tip
Data frequently contains repeated values. DISTINCT gives you clean, deduplicated lists — essential for analysis and reporting.
When to Use
Unique countries, product categories, order statuses, deduplicating email lists.
Find the number of unique categories in the products table. Name the result column unique_categories.
Your Query