01 / 21
DISTINCT

SQL DISTINCT

Learn how to eliminate duplicate rows from your query results and understand when DISTINCT actually changes what you get back.

SQL

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

Without DISTINCT
city
Chicago
Denver
Chicago
Austin
Denver
Chicago
→
With DISTINCT
city
Chicago
Denver
Austin

Why Does DISTINCT Matter?

Accurate Counts
Without DISTINCT, counting cities would give 6 instead of the correct 3 unique values.
Clean Reports
Dropdown menus and reports need one entry per value, not a list full of repeats.
Performance
Returning fewer rows means less data transferred over the network to your application.

The Syntax Rule

SELECT DISTINCT column1, column2, ... FROM table_name;
1
DISTINCT goes immediately after SELECT, before any column names.
2
It applies to the entire row: two rows are duplicates only if every selected column matches.
3
You can use DISTINCT with one column or many columns: the more columns you list, the fewer duplicates you typically have.

Single Column vs. Multiple Columns

DISTINCT city
city
Chicago
Denver
Austin
3 rows
DISTINCT city, state
citystate
ChicagoIL
DenverCO
AustinTX
AustinNV
4 rows

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:

Primary Key
You include the primary key column (e.g., id). Every row already has a unique id.
SELECT *
Selecting all columns often means every row is unique because the primary key is included.
Unique Constraint
A column with a UNIQUE constraint (like email) already has no duplicates.

How to Apply DISTINCT

1
Write your SELECT first. Start with a normal query that returns the columns you need.
2
Run it and check for duplicates. Look at the results: are there repeated rows?
3
Add DISTINCT after SELECT. Place the keyword right after SELECT and before your column list.
4
Verify the row count dropped. If it didn't, DISTINCT wasn't needed: your data was already unique.
5
Consider adding columns. If you need more detail, add columns to SELECT: this may reduce or eliminate duplicates naturally.

Example 1: Unique Departments

Given an employees table, list all departments with at least one employee.

-- Without DISTINCT: duplicates! SELECT department FROM employees; -- With DISTINCT: clean list SELECT DISTINCT department FROM employees;
Without
department
Sales
Sales
Engineering
Engineering
Marketing
→
With DISTINCT
department
Sales
Engineering
Marketing

Example 2: Unique Combinations

Find every unique combination of department and job_title in the company.

SELECT DISTINCT department, job_title FROM employees;
departmentjob_title
SalesManager
SalesRepresentative
EngineeringDeveloper
EngineeringManager
MarketingDesigner

Example 3: DISTINCT inside COUNT

Count how many unique departments exist in the company.

-- Counts all rows (including duplicates) SELECT COUNT(department) AS total FROM employees; -- Result: 5 -- Counts only unique departments SELECT COUNT(DISTINCT department) AS unique_depts FROM employees; -- Result: 3

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.

Without DISTINCT
middle_name
James
NULL
Marie
NULL
NULL
→
With DISTINCT
middle_name
James
NULL
Marie

Combining DISTINCT with ORDER BY

SELECT DISTINCT department FROM employees ORDER BY department ASC;
department
Engineering
Marketing
Sales

Common Mistakes

✕ DISTINCT on SELECT *
Using SELECT DISTINCT * rarely removes duplicates because the primary key makes every row unique.
✕ Thinking per-column
DISTINCT doesn't apply to each column independently. It applies to the entire row combination.
✕ Overusing DISTINCT
Adding DISTINCT "just in case" hides bugs. If you get duplicates, fix the JOIN instead of masking the problem.

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?

SELECT DISTINCT customer, product FROM orders;

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?