Real tables often contain repeated values — several employees in the same department, several orders from the same customer, several products at the same price. The DISTINCT keyword tells SQL to remove duplicate rows from a result set, leaving only unique values behind.

It's most useful when you care about what values exist rather than every individual row that has them — "which departments do we have?" instead of "list every employee's department."

Basic Syntax

SELECT department
FROM employees;

Result (without DISTINCT):

department
------------
Engineering
Sales
Engineering
Sales
Marketing

SELECT DISTINCT department
FROM employees;

Result (with DISTINCT):

department
------------
Engineering
Sales
Marketing

DISTINCT on Multiple Columns

When you list more than one column after DISTINCT, SQL treats the combination of those columns as what needs to be unique — not each column separately:

SELECT DISTINCT department, hire_date
FROM employees;

This returns one row per unique (department, hire_date) pair. Two employees in Engineering hired on different dates would produce two separate rows — DISTINCT only collapses rows that are identical across every selected column.

A Common Mix-Up: DISTINCT vs GROUP BY

DISTINCT and GROUP BY can sometimes produce the same result and are easy to confuse early on, but they answer different questions. SELECT DISTINCT department just deduplicates a list of values. GROUP BY department is built for pairing with aggregate functions — "how many employees per department," "average salary per department" — where DISTINCT alone can't help at all, since it has no way to count or average within each group.

-- DISTINCT: just list unique departments
SELECT DISTINCT department FROM employees;

-- GROUP BY: list departments WITH a calculation per group
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;

Performance Note

DISTINCT requires the database to compare every row against every other row to find duplicates, which can be costly on large tables. If you're filtering with WHERE first to narrow the dataset down, DISTINCT has much less work to do — it's worth filtering before deduplicating whenever possible, rather than relying on DISTINCT to clean up an unfiltered query.