SELECT Distinct

Distinct

The DISTINCT keyword eliminates duplicate records in a  column from the result and returns only the Distinct values.

Syntax: 

SELECT DISTINCT <Column_Name> FROM Table_Name;

Example:

———————————————————————
USE [AdventureWorks2012];
GO

SELECT DISTINCT [OrganizationLevel]

FROM [HumanResources].[Employee];
———————————————————————

The first line of the above statement indicates that we are going to execute the SELECT statement in AdventureWorks2012 database. The SELECT statement fetches all the distinct values from the column OrganizationLevel.

Result set:

Distinct

 Note:
  • In the above example, [HumanResources] is the Schema name and [Employee] is the table. I will explain about schema in my upcoming tutorials. Do not get confused between these two.
  • I am using square brackets to all the columns, Database, Schema and table names in the above example. This query works even without square brackets. But it is a good practice to include square brackets. Because, the query will fail if there is space in the Column/Table name.

Leave a Reply

Your email address will not be published. Required fields are marked *