Careeroza
SELECT DISTINCT
The SQL SELECT DISTINCT Statement
SELECT DISTINCT returns only distinct (different) values. Inside a table a column often contains many duplicates; DISTINCT removes them from the result.
SELECT DISTINCT column1, column2, ...
FROM table_name;Without DISTINCT
Example
Select every country (including duplicates):
SELECT Country FROM Customers;Result: 8 rows
| Country |
|---|
| India |
| UK |
| Canada |
| India |
| USA |
| Norway |
| Singapore |
| UK |
With DISTINCT
Example
Select only the distinct countries:
SELECT DISTINCT Country FROM Customers;Result: 6 rows
| Country |
|---|
| India |
| UK |
| Canada |
| USA |
| Norway |
| Singapore |
DISTINCT on Several Columns
With more than one column, DISTINCT removes rows where the combination of values is repeated.
Example
Distinct department/manager pairs:
SELECT DISTINCT Department, ManagerID
FROM Employees;Result: 5 rows
| Department | ManagerID |
|---|---|
| Sales | NULL |
| Sales | 1 |
| Support | 5 |
| Support | NULL |
| Finance | NULL |
Count Distinct
Example
Count how many different countries we have customers in:
SELECT COUNT(DISTINCT Country) AS Countries FROM Customers;Result: 1 row
| Countries |
|---|
| 6 |
Note: COUNT(DISTINCT column) is not supported in Microsoft Access.