Learn SQL
-- tables.distinct

DISTINCT — Unique Values in SQL

Collapse duplicate rows into one

--concept

SELECT DISTINCT removes duplicate rows from the result. On one column it answers “which different values exist?”; on several columns it keeps unique combinations. Pair it with COUNT to ask “how many different…?” — COUNT(DISTINCT department) is an interview staple.

--how it works
1

Place DISTINCT after SELECT

SELECT DISTINCT department — it applies to the entire output row, not to one column.

2

The engine deduplicates

It compares complete output rows and keeps one copy of each — usually by sorting or hashing the result set.

3

One column → unique values

Six employees become three departments, because duplicates merge.

4

Several columns → unique combinations

SELECT DISTINCT department, location keeps a row per distinct (department, location) pair.

5

Count them instead of listing

COUNT(DISTINCT department) answers "how many different?" without returning rows.

--visualization
employees → department columnEngineeringSalesEngineeringMarketingSalesEngineeringduplicates everywhere — press Run to collapse them
Stage 1 / 4
SELECT DISTINCT department — six rows, but only three different values. Duplicates are about to merge.
--example
query.sql
1SELECT DISTINCT department
2FROM employees;
Result
department
Engineering
Sales
Marketing

Six employee rows collapse into three departments — Engineering appears three times in the source but once in the result. Watch duplicates merge in the visualization.

--variants

Single column

Which different values exist?

query.sql
1SELECT DISTINCT department
2FROM employees;
Result
department
Engineering
Sales
Marketing
1Engineering appears 3× in the source, Sales 2×, Marketing 1×.
2After deduplication each value survives exactly once.
3Three result rows from six source rows.

Multiple columns

Unique combinations across the listed columns.

query.sql
1SELECT DISTINCT department, age
2FROM employees
3ORDER BY department, age;
Result
departmentage
Engineering29
Engineering33
Engineering41
Marketing38
Sales26
Sales35
1No two employees share a department AND age here, so all six pairs survive.
2Add a column and duplicates often reappear — DISTINCT works on whole rows.
3ORDER BY (shown) tidies the result afterwards.

COUNT(DISTINCT …)

The interview staple: count unique values.

query.sql
1SELECT COUNT(DISTINCT department) AS depts
2FROM employees;
Result
depts
3
1The engine first computes the distinct set {Engineering, Sales, Marketing}.
2Then it counts that set — 3.
3COUNT(department) without DISTINCT would have said 6.

NULLs merge too

All NULLs count as one identical value under DISTINCT.

query.sql
1SELECT DISTINCT bonus FROM employees
2ORDER BY bonus;
Result
bonus
NULL
2500
3000
5000
1Three NULL bonuses collapse into a single NULL row.
2DISTINCT is one of the few places NULLs behave as equal.
3ORDER BY sorts the survivors; NULL placement is engine-defined (often first or last).
--key facts
ScopeDISTINCT applies to the whole row combination, not a single column
Costdeduplication requires sorting/hashing — noticeable on huge tables
Count variantCOUNT(DISTINCT department) = 3 on our sample data
--use cases
Filter dropdowns: list the distinct values users can pick from
"How many different X do we have?" — always COUNT(DISTINCT …)
Spotting dimension combinations in analytics data
Cleaning imported data: see the unique categories actually present
--under the hood
Deduplication costs a sort or hash — on huge tables prefer pre-aggregated or indexed designs.
DISTINCT on many wide columns can dwarf the cost of the underlying scan; project only what you need.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.