HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

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:

SELECT DISTINCT City FROM employee;
You can click on above box to edit the code and run again.

Output

distinct city

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

No distinct city

Demo employee table


This employee table is used for the above examples:


employee table

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

count distinct

For Microsoft Access databases.

Example

SELECT Count (*)  AS  DistinctCity 
FROM (SELECT DISTINCT City FROM employee);
You can click on above box to edit the code and run again.

Output

You will learn about the COUNT function later in this tutorial..