TOP

SQL SELECT DISTINCT Instruction

SQL SELECT DISTINCT Description

The instruction SELECT DISTINCT is used to return only distinct (different) values.

Within a table, a column often contains many repeating values; sometimes you just need to list distinct (different) values.


SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name

Demonstration database

The following is a sample from the "Customers" table of the "Northwind" database:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

Example SELECT without DISTINCT

The following SQL statement selects all values ​​(including duplicates) from the "Country" ("Country") column in the "Customers" table:

Run SQLSELECT Country FROM Customers

Now let's use the instruction SELECT DISTINCT and see the result.

SELECT DISTINCT Examples

The following SQL statement selects only distinct values ​​from the "Country" ("Country") column in the "Customers" table:

Run SQLSELECT DISTINCT Country FROM Customers

The following SQL statement displays the number of different (unique) customer countries:

Run SQLSELECT COUNT(DISTINCT Country) FROM Customers