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:
SELECT DISTINCT category
FROM menu_items
ORDER BY category; category
----------
coffee
food
tea
(4 rows)Let's go through the result:
- Of the seven
coffeerows one is left, and the same goes forfoodandtea. - 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. ORDER BYis needed here becauseDISTINCTdoesn't sort the rows. WithoutORDER BY, in our run PostgreSQL returned them in the orderNULL,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:
SELECT DISTINCT category, is_seasonal
FROM menu_items
ORDER BY category, is_seasonal; 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
DISTINCTchanges only the result, andmenu_itemsstill has 13 rows; - a spreadsheet can compare rows by one column and keep the other columns, while
DISTINCTalways 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:
SELECT name, DISTINCT category -- ✗
FROM menu_items;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 DISTINCTkeeps only distinct rows in the result.- With several columns, a row is a repeat when all the selected columns match.
DISTINCTdoesn't sort: if you need an order, addORDER BY.DISTINCTis written right afterSELECT, and it doesn't change the table.