SQL DISTINCT
Learn how to eliminate duplicate rows from your query results and understand when DISTINCT actually changes what you get back.
What is DISTINCT?
When you run a SELECT query, the database returns every row that matches, including duplicate rows. The DISTINCT keyword tells the database to remove those duplicates and return only unique rows.
Duplicates Before vs. After
| city |
|---|
| Chicago |
| Denver |
| Chicago |
| Austin |
| Denver |
| Chicago |
| city |
|---|
| Chicago |
| Denver |
| Austin |
Why Does DISTINCT Matter?
The Syntax Rule
Single Column vs. Multiple Columns
| city |
|---|
| Chicago |
| Denver |
| Austin |
| city | state |
|---|---|
| Chicago | IL |
| Denver | CO |
| Austin | TX |
| Austin | NV |
DISTINCT vs. GROUP BY
| DISTINCT | GROUP BY | |
|---|---|---|
| Purpose | Remove duplicate rows from results | Group rows for aggregation |
| Aggregates? | Only inside functions, e.g., COUNT(DISTINCT col) |
Yes, operates on each group (COUNT, SUM, AVG) |
| Placement | Immediately after SELECT | After WHERE clause |
| Use when | You want a list of unique values | You need to calculate aggregates per group |
When DISTINCT Doesn't Change Results
DISTINCT has no effect when the selected columns already produce unique rows. This commonly happens when:
How to Apply DISTINCT
Example 1: Unique Departments
Given an employees table, list all departments with at least one employee.
| department |
|---|
| Sales |
| Sales |
| Engineering |
| Engineering |
| Marketing |
| department |
|---|
| Sales |
| Engineering |
| Marketing |
Example 2: Unique Combinations
Find every unique combination of department and job_title in the company.
| department | job_title |
|---|---|
| Sales | Manager |
| Sales | Representative |
| Engineering | Developer |
| Engineering | Manager |
| Marketing | Designer |
Example 3: DISTINCT inside COUNT
Count how many unique departments exist in the company.
DISTINCT and NULL Values
An important edge case: DISTINCT treats all NULLs as equal. If five rows have NULL in a column, DISTINCT keeps only one NULL in the result.
| middle_name |
|---|
| James |
| NULL |
| Marie |
| NULL |
| NULL |
| middle_name |
|---|
| James |
| NULL |
| Marie |
Combining DISTINCT with ORDER BY
| department |
|---|
| Engineering |
| Marketing |
| Sales |
Common Mistakes
Quick Reference Summary
| Scenario | Syntax | Effect |
|---|---|---|
| Unique values in one column | SELECT DISTINCT col | Removes duplicate col values |
| Unique row combinations | SELECT DISTINCT col1, col2 | Removes duplicate pairs |
| Count unique values | COUNT(DISTINCT col) | Returns number of unique values |
| DISTINCT on primary key | SELECT DISTINCT id, ... | No effect: id is already unique |
| DISTINCT with NULL | SELECT DISTINCT col | All NULLs collapse to one row |
Question 1 of 5
Which SQL keyword removes duplicate rows from query results?
Question 2 of 5
Given an orders table with columns id, customer, and product, what does this query return?
Question 3 of 5
An employees table has 100 rows. The department column contains 5 unique values. What does COUNT(DISTINCT department) return?
Question 4 of 5
A column has the values: 'A', NULL, 'B', NULL, 'A'. How many rows does SELECT DISTINCT on this column return?
Question 5 of 5
When will adding DISTINCT to a query have no effect on the results?