SQL SELECT DISTINCT

This statement is use to select the distinct value or different value from the database.

In a Table, the column can contain or have same value(duplicate), so to get the unique value or different value we use distinct statement.

 

Syntax:

SELECT DISTINCT column1, column2 FROM table_name;

 

Example Database:

This is  customer  table. In this table, we are showing the customer details form the same location.

id name Location
1 Kailash Delhi
2 Amit Haryana
3 Siddarth Noida
4 Vikas Noida

 

SELECT DISTINCT Example:

The following SQL statement selects only the DISTINCT values from the  location  column in the  customer  table:

SELECT DISTINCT location FROM customer;

Result:

Location
Delhi
Haryana
Noida

 

If you want to show, the number of different (distinct) customer location:

SELECT COUNT(DISTINCT location) FROM customer;

Result:

COUNT(DISTINCT location)
3