SQL Tutorial SQL Advanced SQL Database SQL References

SQL - DISTINCT Keyword



The SQL SELECT DISTINCT statement is used to select different (distinct) data from a database table. It eliminates any duplicate records and fetches only unique records.

Syntax

The syntax for using DISTINCT keyword is given below:

SELECT DISTINCT column1, column2, ....
FROM table_name;

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To fetch distinct Salary data of the employees present in the Employee table, the SQL code is:

    SELECT DISTINCT Salary FROM Employee;
    

    This will produce the result as shown below:

    Salary
    3000
    2750
    2800
    3100
  • To fetch distinct Age data of the employees present in the Employee table, the SQL code is:

    SELECT DISTINCT Age FROM Employee;
    

    This will produce the result as shown below:

    Age
    25
    24
    27
    30
    28