DISTINCT

RoadmapsSQL

Overview

DISTINCT removes duplicate rows from the result set. It operates on the entire selected row — two rows are duplicates only if every column value matches. In the logical query flow: FROM → WHERE → SELECT → DISTINCT → ORDER BY. DISTINCT runs after SELECT has picked the columns, comparing complete rows. SELECT DISTINCT category FROM products ORDER BY category; Products has 6 rows spanning 3 categories. Without DISTINCT you would get 6 rows (Electronics three times, Furniture twice, Stationery once). With DISTINCT, you get 3 rows: Electronics, Furniture, Stationery. DISTINCT on multiple columns deduplicates based on the combination: SELECT DISTINCT country, status FROM customers c JOIN orders o ON o.customer_id = c.id ORDER BY country, status; This returns only the unique (country, status) pairs, not unique countries or unique statuses separately. DISTINCT treats NULLs as equal to each other — if two rows both have NULL in a column, DISTINCT considers them duplicates on that column: SELECT DISTINCT country FROM customers ORDER BY country; Returns 5 rows: AE, JP, MX, UK, US — plus one NULL row for Emma. Even if multiple customers had NULL country, only one NULL row would appear. Mental model: DISTINCT is a row-level deduplication filter that runs after SELECT. It answers 'what unique combinations exist?' — not 'how many of each?' (that's GROUP BY + COUNT).

DISTINCT is essential for extracting the set of unique values — populating a dropdown of countries, listing which categories exist, or checking data quality by finding unexpected duplicates. Note that sorting or hashing rows to find duplicates adds performance overhead.

Where used: populating filter dropdowns in a UI, data quality checks for unexpected duplicates, analytics queries needing unique value lists

Why learn this

Query walkthrough

SELECT DISTINCT category
FROM products
ORDER BY category;

Focus: Watch DISTINCT collapse 6 product rows into 3 unique category values — the duplicates vanish between SELECT and ORDER BY.

Common mistakes

Glossary

duplicate
An exact copy of a row or piece of data that appears more than once in the results.
overhead
The extra work or processing time required by the database system to perform a specific task, like sorting data to find duplicates.
unique
Being the only one of its kind; a value or row that does not appear anywhere else in the results.

Recall questions

Understanding checks

How many rows come back, and what are they?

3 rows: Electronics, Furniture, Stationery.

Products has 6 rows across 3 categories (Electronics x3, Furniture x2, Stationery x1). DISTINCT collapses duplicates, keeping one row per unique category value.

What distinct statuses appear?

3 rows: cancelled, paid, pending.

The 8 orders have statuses: paid (5 times), pending (1), cancelled (1). DISTINCT removes the 4 duplicate 'paid' rows, leaving 3 unique values sorted alphabetically.

Practice tasks

Find unique countries with orders

This query lists all customer countries including duplicates. Modify it to return only the distinct countries by adding the DISTINCT keyword. Keep the ordering.

Challenge

Unique order statuses per customer country

Write a query that returns the distinct (country, status) pairs by joining customers and orders. Order by country, then status. This shows which order statuses exist in each country.

Continue learning

Previous: SELECT, FROM & Columns

Previous: WHERE & Operators

Return to SQL Roadmap