SQL Quest
Sign in Français 0 XP Lvl 1
Beginner

SQL DISTINCT: remove duplicates

3 min read · 3 runnable examples · updated October 9, 2026

When you display the city of every customer, the same city comes back dozens of times. To get the list of distinct values, you add DISTINCT right after SELECT.

Syntax

SELECT DISTINCT column1, column2
FROM table;

How it works

DISTINCT removes duplicate rows from the result. Important point: it applies to the whole row, not to a single column. With SELECT DISTINCT ville, age, two rows are only duplicates if the city and the age are identical.

Counting distinct values

COUNT(DISTINCT column) counts the number of distinct values in a column, ignoring NULLs.

DISTINCT or GROUP BY?

SELECT DISTINCT ville FROM clients and SELECT ville FROM clients GROUP BY ville give the same result. Use DISTINCT to remove duplicates, and GROUP BY when you also want to compute something per group (a count, a sum…).

Diagram

clients
prenomville
LéaLyon
HugoParis
InèsLyon
AdamParis
result
ville
Lyon
Paris
SELECT DISTINCT ville: each city appears only once.

Simplified diagram to show the idea. The examples below use the real SQL Quest practice database (a fictional online shop: see the full schema).

Glossary: the sample database uses French names
clients
customers
commandes
orders
lignes_commande
order lines
produits
products
fournisseurs
suppliers
employes
employees
prenom / nom
first name / last name (or name)
ville
city
pays
country
prix
price
prix_unitaire
unit price
quantite
quantity
categorie
category
date_commande
order date
poste
job title
departement
department
salaire
salary
statut
status: livrée = delivered, expédiée = shipped, en cours = in progress, annulée = cancelled

Examples with their results

1. List the distinct values

The cities where our customers live, without duplicates.

SELECT DISTINCT ville
FROM clients
ORDER BY ville;
Try it in the sandbox
Result
ville
Bordeaux
Bruxelles
Casablanca
Genève
Lille
Lyon
Marseille
Montréal
Nantes
Paris

12 rows in total · 10 shown

2. Count the distinct values

How many different cities, and how many customers in total?

SELECT COUNT(DISTINCT ville) AS villes, COUNT(*) AS clients
FROM clients;
Try it in the sandbox
Result
villesclients
12120

1 row in total

3. DISTINCT on two columns

The distinct (department, job title) pairs among the employees.

SELECT DISTINCT departement, poste
FROM employes
ORDER BY departement;
Try it in the sandbox
Result
departementposte
DirectionPDG
ITDirectrice technique
ITDéveloppeur
RHResponsable RH
RHChargé RH
VentesDirecteur commercial
VentesCommercial

7 rows in total

Common mistakes

Thinking DISTINCT applies to one column

Here, every row is different because the id changes: DISTINCT removes nothing.

Avoid
SELECT DISTINCT id, ville FROM clients;
Correct
SELECT DISTINCT ville FROM clients;

Writing DISTINCT in the middle of the SELECT

DISTINCT goes right after SELECT, not in front of a particular column (except inside COUNT(DISTINCT …)).

Avoid
SELECT prenom, DISTINCT ville FROM clients;
Correct
SELECT DISTINCT ville FROM clients;

Hiding a join problem

Unexpected duplicates after a join often point to an incomplete ON condition. DISTINCT hides them without fixing the cause: check your join first.

Avoid
SELECT DISTINCT c.nom FROM clients c JOIN commandes o;
Correct
SELECT DISTINCT c.nom FROM clients c JOIN commandes o ON o.client_id = c.id;

Exercises

Try writing the query yourself before opening the solution.

Challenge 1. Show the list of distinct order statuses.

Show a solution
SELECT DISTINCT statut FROM commandes;
statut
annulée
en cours
expédiée
livrée

4 rows in total

Try it in the sandbox

Challenge 2. How many different customers have placed at least one order?

Show a solution
SELECT COUNT(DISTINCT client_id) FROM commandes;
COUNT(DISTINCT client_id)
110

1 row in total

Try it in the sandbox
Want more practice?

The interactive exercises are checked automatically, with a tutor who explains your mistakes. A short lesson stays next to the editor.

Do the interactive exercises

Frequently asked questions

Does DISTINCT sort the results?

Not necessarily. The order may look sorted, but it is not guaranteed: add ORDER BY if the order matters.

Does DISTINCT take NULLs into account?

Yes, SELECT DISTINCT treats all NULLs as identical and keeps one of them. COUNT(DISTINCT column), however, ignores NULLs.

Does DISTINCT slow down a query?

A little, because the database has to compare the rows with each other. On small tables, it is negligible.

Read next