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 DISTINCT can 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

ExampleWhat 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!