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, column2FROM table_name;The examples use the Productions and Invoices tables from the example database.
With and without DISTINCT
Section titled “With and without DISTINCT”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 |
Several columns
Section titled “Several columns”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.
Counting different values
Section titled “Counting different values”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.