Skip to content
Laviyx Learn

Try

SQL course menu

SQL DISTINCT: Remove Duplicate Rows From Results

Learn how SQL SELECT DISTINCT removes duplicate values, how it works with multiple columns, how to count unique values with COUNT(DISTINCT), and when not to use it.

Beginner6 min readUpdated
On this page

When you select a column, you get one value per row, including repeats. If five employees work in three departments, SELECT department returns five rows. DISTINCT gives you each value once.

The example table#

idnamedepartmentsalarycity
1AshaSales52000Mumbai
2RaviEngineering78000Bengaluru
3MeeraEngineering81000Pune
4JohnMarketing46000Mumbai
5PriyaSales61000Delhi

Without DISTINCT#

SQLRun
SELECT department
FROM employees;
Output
department
-----------
Sales
Engineering
Engineering
Marketing
Sales

With DISTINCT#

SQLRun
SELECT DISTINCT department
FROM employees;
Output
department
-----------
Sales
Engineering
Marketing

DISTINCT goes right after SELECT and removes duplicate rows from the result. The table itself is not changed.

Syntax#

SQL
SELECT DISTINCT column1, column2, ...
FROM table_name;

DISTINCT with several columns#

With more than one column, DISTINCT removes rows where the whole combination is repeated, not each column separately:

SQLRun
SELECT DISTINCT department, city
FROM employees
ORDER BY department, city;
Output
department  | city
------------+----------
Engineering | Bengaluru
Engineering | Pune
Marketing   | Mumbai
Sales       | Delhi
Sales       | Mumbai

"Engineering" appears twice because it pairs with two different cities. Every row in the result is a unique pair.

Sorting distinct values#

The order of rows from DISTINCT is not guaranteed. Add ORDER BY when order matters:

SQLRun
SELECT DISTINCT city
FROM employees
ORDER BY city;
Output
city
---------
Bengaluru
Delhi
Mumbai
Pune

Count unique values#

Put DISTINCT inside COUNT to count how many different values exist:

SQLRun
SELECT COUNT(DISTINCT city) AS cities
FROM employees;
Output
cities
------
4

Five employees live in four different cities, because Asha and John are both in Mumbai.

How DISTINCT treats NULL#

DISTINCT treats all NULLs as equal, so they collapse into a single NULL row. COUNT(DISTINCT column), however, ignores NULLs completely, like every aggregate function except COUNT(*).

DISTINCT vs GROUP BY#

These two queries return the same rows:

SQLRun
SELECT DISTINCT department FROM employees;

SELECT department FROM employees GROUP BY department;

Use DISTINCT when you just want unique values. Use GROUP BY when you also want to calculate something per group, such as a count or an average.

Common mistakes#

Practice#

Practice exercise

Write two queries:

  1. A list of every city where an employee lives, each city once, in alphabetical order.
  2. The number of different departments.
Show hint

The first needs SELECT DISTINCT plus ORDER BY. The second needs COUNT(DISTINCT ...).

Show solution
SQLRun
SELECT DISTINCT city
FROM employees
ORDER BY city;
Output
city
---------
Bengaluru
Delhi
Mumbai
Pune
SQLRun
SELECT COUNT(DISTINCT department) AS departments
FROM employees;
Output
departments
-----------
3

Key takeaway#

Finished this lesson?