SQL Tutorial – DISTINCT keyword in SQL.

Spread the love

You have a table and you only want to select distinct or unique values from it. For example, in the CountryCode column, you can see there are multiple rows of data for the same country but you want to only select the distinct country code from this or you have a column which contains duplicate data and you want to select only the unique values from it.

Solution –

Use the DISTINCT keyword to select only unique values from a column. We use DISTINCT keyword immediately after the SELECT statement.

SELECT DISTINCT CountryCode
FROM city;

There are 232 distinct or unique countries in this table, only few of them are shown here.

You can also use distinct with multiple columns. Let’s say you wan to find out for each countries in this table, what are all unique district values in the District column.


SELECT DISTINCT CountryCode, District
FROM city;

For Afghanistan ( AFG ) we have 4 unique districts Kabul, Qandahar, Herat, and Balkh data in this table.

Rating: 1 out of 5.

Leave a Reply