SQL Server - DISTINCT Keyword
The SQL Server (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 SQL Server (Transact-SQL) is given below:
SELECT DISTINCT column1, column2, .... FROM table_name;
Example:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
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
❮ SQL Server Keywords