SQL SELECT DISTINCT

SQL DISTINCT clause is used to remove duplicate records.

SELECT DISTINCT returns distinct(different) values.

DISTINCT can be used with aggregate functions: COUNT, AVG, MAX, MIN.

Syntax:

SQL SELECT DISTINCT

SQL SELECT DISTINCT based on one column

You can SELECT DISTINCT values from one column in a table.

Below is the syntax.

SQL SELECT DISTINCT

SQL SELECT DISTINCT based on multiple columns

You can SELECT DISTINCT values from multiple columns in a table.

Below is the syntax.

SQL SELECT DISTINCT

SELECT DISTINCT

Example:

Question 1:

From AdventureWorks2016 database, Select AverageRate column(field) from Sales.CurrencyRate table.

Query:

SQL SELECT DISTINCT

Result:

SQL SELECT DISTINCT

Question 2:

From AdventureWorks2016 database, Select Distinct AverageRate column(field) from Sales.CurrencyRate table.

Query:

SQL SELECT DISTINCT
Source: Datacatchup

Result:

Note:

Question 1 result pulled duplicate records (1.00)   while in Question 2 result no duplicate record was pulled.

Total number of records pulled

Question 1 – 13532 rows

Question 2 – 6127 rows

In summary, the DISTINCT keyword is very helpful when analyzing data by removing duplicates and eliminating data redundancy.

Please follow and like us:

Leave a Reply

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