SQL SELECT DISTINCT
SQL Select Distinct Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
- SELECT DISTINCT eliminates duplicate records from the results.
- DISTINCT can be used with aggregates - COUNT, AVG, MAX, etc.
- DISTINCT operates on a single column.
- DISTINCT for multiple columns is not supported.
SQL DISTINCT keyword
The SQL DISTINCT keyword is used in conjunction with the SELECT statement to eliminate all the duplicate records and fetching only unique records.
SQL SELECT DISTINCT syntax
General Syntax
SELECT DISTINCT column_name
FROM table_name;
Syntax with COUNT
SELECT COUNT (DISTINCT column_name)
FROM table_name;
Employee Table
A sample of EMPLOYEE records table & the name of table Employee. The column(s) of the Employee table [ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, ADDRESS, CITY, and COUNTRY]:
ID |
EMPLOYEE_NAME |
EMPLOYEE_EMAIL |
ADDRESS |
CITY |
COUNTRY |
1 |
Mark J. |
mark@gmail.com |
House 53, Sq. |
London |
U.K. |
2 |
Lack Josh |
lackjosh@gmail.com |
2745 road mark |
Delhi |
India |
3 |
Kim Juhu |
kimjuhu@gmail.com |
Street 587327, 34 House |
Seoul |
South Korea |
4 |
Jack Jenifer |
jenifer@gmail.com |
464673 Mataderos |
Mexico D.F. |
Mexico |
5 |
McKinley |
mckinley@gmail.com |
54, Road Washington |
Washington |
U.S. |
6 |
Alena |
aln@gmail.com |
House No. 6563 |
Tokyo Japan |
Japan |
7 |
Mack Marina |
mmk@gmail.com |
43 Road, NH |
San Francisco |
U.S. |
8 |
Dipak Jha |
jha@gmail.com |
Mahanager, 26 Road |
Lucknow |
India |
SELECT DISTINCT Example
SELECT DISTINCT Country
FROM Employee;
Result
Country |
U.K. |
India |
South Korea |
Mexico |
U.S. |
Japan |