T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL - DISTINCT Keyword



The T-SQL (Transact-SQL) DISTINCT keyword 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 in T-SQL (Transact-SQL) 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 query 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 query is:

    SELECT DISTINCT Age FROM Employee;
    

    This will produce the result as shown below:

    Age
    25
    24
    27
    30
    28

❮ T-SQL Keywords