SelectFromData
AnalyticsLesson 5 of 12 · 10 min

DISTINCT: Each Value Once

"What departments do we even have?" "Which countries do we ship to?" Questions about the unique values in a column come up constantly — and DISTINCT answers them in one word.

In one line

SELECT DISTINCT collapses duplicate rows into one, leaving each distinct value exactly once.

See it

Five employees, but only three departments — the repeats fold away:

department
Engineering
Sales
Engineering
Sales
Marketing
DISTINCT department
department
Engineering
Sales
Marketing
Rows that repeat a value already seen are removed; one row survives per distinct value.

Try it now — write the query that produces that list, returning each department once.

Your turn · Exercise

Return the distinct list of departments (each department once).

The data

employees

5 rows returned

namedepartment
AdaEngineering
BoSales
CyEngineering
DiSales
EliMarketing
Runs in your browser

It applies to the whole row

One subtlety worth knowing early: DISTINCT de-duplicates the entire selected row, not one column. SELECT DISTINCT department, city returns unique combinations of department and city — which may be more rows than unique departments alone. Run it and count:

Unique department+city combinations — more than 3 rows
Editable · runs in your browser

From practice

When a list looks "too long" or has obvious repeats, SELECT DISTINCT is the fastest sanity check there is. Later, in the aggregation lesson, you'll meet its counting cousin: COUNT(DISTINCT col).

Check yourself

SELECT DISTINCT department, city returns…


That's the full result-shaping toolkit: SELECT … FROM picks columns, WHERE picks rows, ORDER BY sorts, LIMIT cuts, DISTINCT de-duplicates. Next: filtering with more than one condition — ranges, lists, text patterns, and the tricky case of missing values.