SQL SELECT DISTINCT – Removing Duplicates from Table = Unique values

SELECT DISTINCT is a command used to show unique records in a table. Its use is broad but it is mainly used to remove duplicities in the records when taking multiple views on data.

Hint: If we select more than 1 column, DISTINCT will return unique combinations of selected columns

SELECT DISTINCT Syntax

Syntax is easy, all you need to do is add magic word “DISTINCT” right after SELECT clause.

SELECT DISTINCT [Column 1], [Column 2]
FROM [Table];

and the result will be unique combinations of columns

Example of extracting unique records from SQL table

Source table looks like this:

Tabulka-Territories

Let’s try to extract unique records from field TerritoryCountry. There are duplicities in this field because 1 country (SalesTerritoryCountry)  can have multiple regions (SalesTerritoryRegion). However, we are interested in unique values.

SELECT DISTINCT [SalesTerritoryCountry]
FROM [AdventureWorksDW2012].[dbo].[DimSalesTerritory]
ORDER BY [SalesTerritoryCountry];

We get this table as required:  

Select distinct priklad

Rate this post
Category: SQL Clauses

About Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zednicek, and I have been working as a freelance Data Engineer for roughly 10 years. During this time, I have been publishing case studies and technical guides on this website, targeting professionals, students, and enthusiasts interested in Data Engineering particularly on Microsoft technologies as well as corporate finance and reporting solutions. 🔥 If you found this article helpful, please share it or mention me on your website or Community forum

Leave a Reply

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