• 5. 11. 2019
  • Ing. Jan Zedníček - Data & Finance
  • 0

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
Tags:

Ing. Jan Zedníček - Data & Finance

My name is Jan Zedníček and I work as a freelancer. I have been working as a freelancer for many companies for more than 10 years.
I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

Leave a Reply

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