DISTINCT in SQL Server

Distinct is a clause which is used along with SELECT Command to eliminate duplicate values from the result set.

Generally some of the information is duplicated in the table. In order to fetch unique or non duplicate values DISTINCT Clause will be used in the SELECT Command.

Syntax for DISTINCT Clause is:

SELECT DISTINCT <Column list > from <Table Name>

Example:

SELECT * FROM MYORDERS WHERE OrderId BETWEEN 100001 and 100004


MYORDERS table have duplicate orders for the OrderIds between 100001 and 100004.

So, in order to eliminate the duplicate values or duplicate records from the DISTINCT clause should be used:

SELECT DISTINCT * FROM MYORDERS WHERE OrderId BETWEEN 100001 and 100004

Below picture contains the result set with and without DISTINCT Clause:

This entry was posted in . Bookmark the permalink.

Leave a reply