DISTINCT
Removes duplicate rows from the query result.
Overview
DISTINCT removes duplicate rows based on the complete set of selected columns. With one column it returns unique values; with several columns it removes only identical combinations. Do not use DISTINCT to hide duplicates caused by an incorrect JOIN; investigate their business meaning first.
Basic syntax
SELECT DISTINCT department FROM employees;
Examples
DISTINCT example
Return the unique department values from employees and compare them with the repeated source values.
Sample table
employees| department |
|---|
| Sales |
| IT |
| Sales |
| HR |
Run result
| department |
|---|
| Sales |
| IT |
| HR |
Common issues
It compares the complete combination of selected columns, not only the first column.
Duplicate selected-column combinations containing NULL are generally reduced to one result combination.
Use DISTINCT for unique combinations and GROUP BY when you need aggregates per group.
Tags
Was this explanation clear?
Thanks. This feedback will inform the next content review.