The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Example
Select all the different city from the "Employee" table:You can click on above box to edit the code and run again.
SELECT DISTINCT City FROM employee;
Output
In a table, a column contains many duplicate values; but it show only the list of different (distinct) values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
SELECT Example Without DISTINCT
If you do not use the DISTINCT keyword, the SQL statement returns the "City" value from all the records of the "employee" table:
Example
SELECT City FROM employee;You can click on above box to edit the code and run again.
Output
Demo employee table
This employee table is used for the above examples:
Count Distinct
By using the DISTINCT keyword in a function called COUNT , we can return the number of different cities.
Example
SELECT COUNT( DISTINCT City) FROM employee;You can click on above box to edit the code and run again.
Output
For Microsoft Access databases.
You will learn about the COUNT function later in this tutorial..