Tutorials › SQL Tutorial › SQL SELECT DISTINCT

SQL SELECT DISTINCT

7 min read · Lesson 4 of 24

A column often contains the same value many times. Twelve customers, but only a handful of countries. Twelve products, but only six categories. SELECT DISTINCT returns each different value only once.

Syntax

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

The problem DISTINCT solves

First, look at the country of every customer:

Example SQL
SELECT country FROM customers;
Try it Yourself »

Kenya appears again and again. If you only want to know which countries Skope Shop sells to, that repetition is noise. Add DISTINCT:

Example SQL
SELECT DISTINCT country FROM customers;
Try it Yourself »

Now each country appears once. Notice that the empty (NULL) country of Nyeri Stationers also appears once. DISTINCT treats all NULLs as one group.

DISTINCT on several columns

When you list more than one column, DISTINCT looks at the combination of values. A row is removed only if every listed column matches another row.

Example SQL
SELECT DISTINCT city, country
FROM customers;
Try it Yourself »

Kisumu appears once here, because both Kisumu customers share the same city and country. But if two cities had the same name in different countries, both would be kept.

Another example: which order statuses are in use?

Example SQL
SELECT DISTINCT status FROM orders;
Try it Yourself »
Note: DISTINCT always applies to the whole row of the result, not to just the first column after it. SELECT DISTINCT city, country does not mean "distinct city, plus any country".

Counting distinct values

A very common question is "how many different X are there?". You can put DISTINCT inside the COUNT() function:

Example SQL
SELECT COUNT(DISTINCT category) FROM products;
Try it Yourself »

The answer is 6. Without DISTINCT, COUNT(category) would count all 12 products. You will learn more about COUNT in Aggregate Functions.

Here is a practical one: how many different customers have placed at least one order?

Example SQL
SELECT COUNT(DISTINCT customer_id) FROM orders;
Try it Yourself »

There are 12 orders, but they come from only 9 customers. That tells the sales team that some customers order repeatedly and three customers have never ordered at all.

DISTINCT and performance

To remove duplicates, the database has to compare rows with each other, usually by sorting or hashing them. On a table of millions of rows this takes real work. Use DISTINCT when you genuinely need unique values, not as a habit to "clean up" results.

Warning: If a query returns duplicates you did not expect, adding DISTINCT hides the symptom but not the cause. The usual cause is a join that matches more rows than you thought. Fix the query first.

Summary

  • SELECT DISTINCT removes duplicate rows from the result.
  • With several columns, duplicates are judged on the combination.
  • COUNT(DISTINCT column) counts the different values.
Exercise

List each product category only once.

SELECT  category FROM products;
Exercise

Count how many different cities customers come from.

SELECT COUNT( city) FROM customers;
Sign in or create a free account to track progress, earn XP, badges and a certificate.