Home » T-SQL Distinct keyword

T-SQL Distinct keyword

by Online Tutorials Library

T-SQL Distinct keyword

In T-SQL, the DISTINCT keyword is used with the SELECT statement to eliminate duplicate records and give only the unique documents.

Below is the situation where we have many duplicate records in the table. When we fetch records, it makes more sense to bring only unique documents in place of duplicates.

Syntax:

The syntax of the DISTINCT keyword is given below:

Example:

EMPLOYEES TABLE is given below:

ID NAME AGE ADDRESS SALARY
01 William 32 Karachi 7000.00
02 Avery 24 London 3000.00
03 Monty 34 New York 1200.00
04 Harper 20 New York 1500.00
05 Ella 22 Islamabad 4400.00
06 Monty 23 Turkey 4400.00
07 Mason 26 Saudi Arabia 5050.00

Let us see how the SELECT query returns duplicate salary records.

This command produces the below output where the salary 4400 comes twice, which is a duplicate record from the real table.

T-SQL Distinct keyword

Let us use the DISTINCT keyword with the above SELECT query and then see the result.

The command produces the below output, where we have not any duplicate entry.

T-SQL Distinct keyword


Next TopicT-SQL JOINS

You may also like