Skip to content

SQL DISTINCT Clause

Syntax

The DISTINCT clause in SQL is used to remove duplicate rows from the result set, returning only unique values.

-- Retrieve unique values from a single column
SELECT DISTINCT column1
FROM table_name;
-- Retrieve unique rows based on multiple columns
SELECT DISTINCT column1, column2
FROM table_name;

Example

-- Retrieve unique departments
SELECT DISTINCT department
FROM employees;
/* Output:
| department |
|-------------|
| Sales |
| Marketing |
| HR |
*/
-- Retrieve unique combinations of department and age
SELECT DISTINCT department, age
FROM employees;
/* Output:
| department | age |
|-------------|-----|
| Sales | 30 |
| Marketing | 25 |
| HR | 40 |
*/