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 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 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]

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

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]

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).

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 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]

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]
