When DISTINCT is applied to multiple columns, SQL returns unique combinations of the values across all selected columns.
Understanding this behavior prevents confusion when trying to identify unique records based on several attributes.
Find unique combinations of department and location.
SELECT DISTINCT
Department,
Location
FROM Employees;
Analysts use this technique to explore combinations such as region-product, department-location, or customer-category.
- DISTINCT applies to the complete selected combination.
- Duplicate combinations are removed.
- A row can still appear multiple times if another selected value differs.
- It is useful for data exploration.
- It does not modify the source table.
How does DISTINCT work with multiple columns?
It returns unique combinations of all selected column values.
Does DISTINCT make each column individually unique?
No. DISTINCT makes the complete combination of selected columns unique.