SQL SELECT DISTINCT Statement

The SQL SELECT DISTINCT Statement retrieves unique records (by removing the duplicates) from the specified column in the SELECT Statement. The syntax of the SQL Server SELECT DISTINCT is:

-- SQL Server SELECT DISTINCT Statement
SELECT DISTINCT [Column Names]
FROM Source
WHERE Conditions -- This is Optional
  • DISTINCT: Returns Unique Columns in SQL
  • Columns: It allows us to pick the number of columns from the tables. It may be one or more.
  • Source: One or more tables in the Database. JOINS are used to join multiple tables in SQL Server.

We use the below data to explain the SQL Server Select Distinct.

SQL DISTINCT Query 1

SQL Select Distinct Single Column

In this example, We Select the unique records present in the Education Column.

-- SQL Select Distinct on Single Column Example
USE [SQLTEST]
GO
SELECT DISTINCT [Education]
  FROM [Employees]
SQL DISTINCT on One Column 1

SQL Select Distinct Multiple Columns

When we use the Select Distinct multiple columns, the SELECT Statement returns the unique combination of multiple columns instead of unique individual records. In this example, we select the unique combination records present in the Education Column and Yearly Income Column.

-- SQL Select Distinct Multiple Columns Example
SELECT DISTINCT [Education]
 ,[YearlyIncome]
  FROM [Employees]
SQL DISTINCT Multiple Columns 1

Although we used the DISTINCT Keyword in the SELECT Statement, it is returning duplicates because

  • Bachelors and 70000 is a Unique Combination
  • Bachelors and 80000 is a Unique Combination
  • and Bachelors and 90000 is a Unique Combination

SQL DISTINCT WHERE Clause

Using the SQL distinct along with WHERE Clause. The following SQL unique statement will return the Distinct Education values whose Yearly Income is greater than 50000

-- SQL Select Distinct Where Example
SELECT DISTINCT [Education]
 ,[YearlyIncome]
  FROM [Employees]
  WHERE YearlyIncome > 50000
SQL DISTINCT Where Clause 1

The SQL DISTINCT considers the NULL records as a valid unique record. So, Please use any Not Null function (NOT NULL) functions to remove NULLS

SQL Distinct Group By Example

The SQL Server Distinct also allows us to use Aggregate Functions along with Group By Clause. The following Sql unique query groups the employees by Education. Next, it finds the Sum of Distinct Yearly Income for each Education group.

-- SQL Select Distinct Group By Example
SELECT [Education]
 ,SUM(DISTINCT  YearlyIncome) AS [Total Income]
  FROM [Employees]
  GROUP BY [Education]
SQL DISTINCT Group By 1

Let us remove the Distinct Keyword from the above query.

-- SQL Select Distinct Group By Example
SELECT [Education]
 ,SUM(YearlyIncome) AS [Total Income]
  FROM [Employees]
  GROUP BY [Education]

See the difference in yearly Income. Because it is finding the sum of all the records (not the Distinct ones).

SQL DISTINCT Group By 2

Distinct Count Example 1

This SQL Select Distinct Count is one of the most commonly asked questions. This example counts and returns the distinct employee Ids in each Education group.

-- SQL Select Distinct Count Example
SELECT [Education]
 ,COUNT(DISTINCT EmpID) AS [Total Employees]
  FROM [Employees]
  GROUP BY [Education]
SQL DISTINCT Count 1

SQL Select Distinct Count Example 2

Here, we used Count Function with the Distinct Keyword, and without Distinct. This SQL unique example shows the difference between the result set.

-- SQL Select Distinct Count Example
SELECT [Education]
 ,COUNT(DISTINCT DeptID) AS [Total Dept Ids]
 ,COUNT(DeptID) AS [Total Ids]
  FROM [Employees]
  GROUP BY [Education]
SQL DISTINCT Count 2

Select Distinct Null Example

The SQL Server Select Distinct statement considers NULL as a distinct one.

-- SQL Select Distinct Example
SELECT DISTINCT DeptID
  FROM [Employees]
SQL DISTINCT NULL 1

Comments are closed.