SQL SELECT DISTINCT Statement
How do I return unique values in SQL?
SELECT DISTINCT returns only distinct (i.e. different) values.
The DISTINCT keyword eliminates duplicate records from the results.
DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
It operates on a single column. DISTINCT for multiple columns is not supported.
The SQL SELECT DISTINCT syntax
The general syntax is
SELECT DISTINCT column-name FROM table-name
Can be used with COUNT and other aggregates
SELECT COUNT (DISTINCT column-name) FROM table-name
SUPPLIER |
---|
Id |
CompanyName |
ContactName |
City |
Country |
Phone |
Fax |
SQL SELECT Examples
Problem: List all unique supplier countries in alphabetical order.
SELECT DISTINCT Country FROM Supplier ORDER BY COUNTRY
Result: 16 rows
Country |
---|
Australia |
Brazil |
Canada |
Denmark |
![]() |
SUPPLIER |
---|
Id |
CompanyName |
ContactName |
City |
Country |
Phone |
Fax |
Problem: List the number of unique supplier countries
SELECT COUNT (DISTINCT Country) FROM Supplier
Result:
Count |
---|
16 |