Memra

Removing duplicates with DISTINCT

◈ 2 cards

Get the set of distinct values in a column.

The distinct values

Many books share a genre. To get each genre once, use DISTINCT:

SELECT DISTINCT genre FROM books;

DISTINCT removes duplicate rows from the result. With several columns it dedupes on the whole row — SELECT DISTINCT genre, author_id gives each distinct pair.

genrerows in booksafter DISTINCTfiction31science21poetry21history21children1110 rows in, 5 genres out.
DISTINCT dedupes whole result rows. Over one column that is the set of values; over two columns it keeps each distinct pair, so adding a column can only ever grow the answer.
NORMAL ~/memra/learn/postgresql/distinct utf-8 LF