Removing Duplicates with DISTINCT
Return only unique rows with DISTINCT
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
DISTINCT removes duplicate rows from the result set.
SELECT DISTINCT city
FROM users;This returns each unique city name once, no matter how many users live there.
With multiple columns, DISTINCT applies to the *combination* — rows are unique only if every selected column matches: ``sql SELECT DISTINCT department, city FROM employees; -- Returns each unique (department, city) pair once
Combine with ORDER BY to get a sorted list of unique values: ``sql SELECT DISTINCT city FROM users ORDER BY city ASC;
Performance note: DISTINCT requires comparing every row to find duplicates, which can be slow on large tables — only use it when you actually expect duplicates.
Examples
Unique department/city pairs
Each unique combination of department and city appears once, sorted by department
SELECT DISTINCT department, city
FROM employees
ORDER BY department;How well did you understand this?
Next in SQL & Databases
SQL JOINs