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:

IDNAMEAGEADDRESSSALARY
01William32Karachi7000.00
02Avery24London3000.00
03Monty34New York1200.00
04Harper20New York1500.00
05Ella22Islamabad4400.00
06Monty23Turkey4400.00
07Mason26Saudi Arabia5050.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