SQL SELECT DISTINCT
Sometimes, your query results have duplicates. To get only unique (different) values, we use DISTINCT.
๐น How to Use DISTINCT
-- Get unique departments from employees
SELECT DISTINCT department FROM employees;
-- Multiple columns: unique combinations of those columns
SELECT DISTINCT department, job_title FROM employees;๐ง What DISTINCT Does
- Removes duplicate rows from the result
- Works on all selected columns combined (if multiple columns selected)
โ ๏ธ Notes
- Using
DISTINCTcan slow down queries on large data sets โ use only when needed - To remove duplicates on a single column, just select that column with
DISTINCT
๐งฉ Quick Recap
| Example | What it does |
|---|---|
SELECT DISTINCT department FROM employees; | List of unique departments |
SELECT DISTINCT department, job_title FROM employees; | Unique department & job_title combos |
โ
Simple and useful to clean up your data output!