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]
and the result will be unique combinations of columns
Example of extracting unique records from SQL table
Source table looks like this:
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]
ORDER BY [SalesTerritoryCountry];
We get this table as required: