SQL SELECT DISTINCT – Removing Duplicates from Table = Unique values

Last modified date:

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

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Clauses Tags:

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.