Skip to content
Theory · Selecting and sorting · lesson 2 of 10

DISTINCT

Say you need to know which categories the menu has. The query SELECT category FROM menu_items returns 13 rows: coffee seven times, food three times, tea twice and one empty cell. There are only four distinct values, but 13 rows.

In a spreadsheet you would copy the column and press "Remove duplicates": one row would stay for each value. In SQL the word DISTINCT does the same.

DISTINCT removes repeats

DISTINCT goes right after SELECT. It keeps only distinct rows in the result:

SQL
SELECT DISTINCT category
FROM menu_items
ORDER BY category;
TEXT
 category
----------
 coffee
 food
 tea

(4 rows)

Let's go through the result:

  1. Of the seven coffee rows one is left, and the same goes for food and tea.
  2. The empty cell at the bottom is the category of hot chocolate. Nobody filled it in, and such a missing value is called NULL. Chapter 4 will cover it.
  3. ORDER BY is needed here because DISTINCT doesn't sort the rows. Without ORDER BY, in our run PostgreSQL returned them in the order NULL, food, coffee, tea, and that order may change.

Prices work the same way. The menu has 13 items, but SELECT DISTINCT price returns 12 rows: cappuccino and latte cost the same, 220.00, and that price makes it into the result once.

Several columns

If several columns follow DISTINCT, it compares whole rows. Two rows count as a repeat only when all their columns match:

SQL
SELECT DISTINCT category, is_seasonal
FROM menu_items
ORDER BY category, is_seasonal;
TEXT
 category | is_seasonal
----------+-------------
 coffee   | f
 coffee   | t
 food     | f
 tea      | f
 tea      | t
          | f
(6 rows)

coffee appears twice: regular coffee (f) and the seasonal pumpkin latte (t). These are different combinations, so DISTINCT kept both rows. The six regular coffee items gave one row, coffee | f.

Sorting by two columns works like this here: first by category, and within one category by is_seasonal. We'll go through it in detail later in this chapter.

This leads to a consequence: DISTINCT applies not to the first column but to the whole result row. The query SELECT DISTINCT category, name returns all 13 items, since their names differ. Interviewers like to check exactly this.

When DISTINCT works

In terms of meaning, DISTINCT acts after SELECT. First each row keeps only the listed columns, then equal rows merge into one. That is why repeats are looked for among what you selected, not among all columns of the table.

The spreadsheet analogy breaks in two places here:

  • "Remove duplicates" erases rows from the sheet itself, while DISTINCT changes only the result, and menu_items still has 13 rows;
  • a spreadsheet can compare rows by one column and keep the other columns, while DISTINCT always compares all selected columns.

Common mistake

DISTINCT was put before one column in the middle of the list, to remove repeats in that column alone:

SQL
SELECT name, DISTINCT category -- ✗
FROM menu_items;
TEXT
ERROR:  syntax error at or near "DISTINCT"

The DBMS cannot parse the query. DISTINCT stands only right after SELECT and acts on the whole row. If you need distinct categories, select category alone, without name.

Key points

  • SELECT DISTINCT keeps only distinct rows in the result.
  • With several columns, a row is a repeat when all the selected columns match.
  • DISTINCT doesn't sort: if you need an order, add ORDER BY.
  • DISTINCT is written right after SELECT, and it doesn't change the table.