Skip to content

SQL Select Distinct

SELECT DISTINCT returns each different value once. Where a column repeats the same value across many rows, it collapses the repeats into a single row.

SELECT DISTINCT column1, column2
FROM table_name;

The examples use the Productions and Invoices tables from the example database.

A plain SELECT returns one row for every row in the table, repeats included:

SELECT Format FROM Productions;
Format
Drama series
Feature film
Feature film
Drama series
Documentary

Adding DISTINCT keeps one of each:

SELECT DISTINCT Format FROM Productions;
Format
Drama series
Feature film
Documentary

The same works for any column. Kestrel Catering has sent two invoices, but appears once in the list of suppliers:

SELECT DISTINCT Supplier FROM Invoices;
Supplier
Hartwell Lighting
Kestrel Catering
Ashby Camera Hire

With more than one column, DISTINCT removes rows whose values match in every column listed. SELECT DISTINCT Format, Country would keep a feature film made in the UK and a feature film made in Ireland as two separate rows.

To count how many different values a column holds, put DISTINCT inside COUNT:

SELECT COUNT(DISTINCT Format) FROM Productions;

This returns 3: drama series, feature film and documentary.