SELECT DISTINCT removes duplicate rows from the result — it keeps only unique combinations of the selected columns.
Syntax
SELECT DISTINCT column1, column2
FROM table_name;
Sample Table: employees
| id | name | department |
|---|---|---|
| 1 | Aman | Engineering |
| 2 | Riya | Marketing |
| 3 | Karan | Engineering |
| 4 | Neha | Marketing |
Example
SELECT DISTINCT department
FROM employees;
Output:
| department |
|---|
| Engineering |
| Marketing |
Four rows collapse to two — one per unique department.
DISTINCT on Multiple Columns
SELECT DISTINCT department, salary
FROM employees;
This keeps a row only if the combination of department and salary is unique — not just the department alone. Easy to misread if you're used to DISTINCT on a single column.
Practical Use Case
Finding the list of distinct categories, cities, or statuses present in a table — e.g. "which departments actually have employees right now?" before building a filter dropdown in an app.
Common Mistakes
- Using
DISTINCT *on a wide table when only a couple of columns are actually relevant — this can be needlessly slow - Assuming
DISTINCTremoves duplicates per column independently — it deduplicates the whole row of selected columns together - Reaching for
DISTINCTto "fix" a query that's actually producing duplicates because of an incorrect join — the real fix is the join logic, not papering over it with DISTINCT
Interview Relevance
A common trick question: "Will SELECT DISTINCT name and SELECT DISTINCT name, department return the same number of rows?" — not necessarily; the second can return more rows if the same name appears with different departments.
Practice Question
Write a query to list every distinct (department, job_title) combination that currently exists in an employees table.