The SQL SELECT DISTINCT Statement

Within a table, a column might hold duplicate values; and sometimes you only want a list of different (distinct) values. The distinct statement in SQL can be used for the purpose of only seeing the distinct values.

SELECT DISTINCT Syntax

SELECT DISTINCT column1, column2, …
FROM table_name


Demo Database

The following table below is a sample of the “Customers” table in the Northwind database:

SELECT Example Without DISTINCT

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

Example

SELECT Country FROM Customers;

Now, let us use the SELECT DISTINCTstatement and see the result.

SELECT DISTINCT Examples

The following SQL statement selects only the DISTINCT values from the “Country” column in the “Customers” table:

Example

SELECT DISTINCT Country FROM Customers;

The following SQL statement lists the number of different (distinct) customer countries:

Example

SELECT COUNT(DISTINCT Country) FROMCustomers;

Here is the workaround for MS Access:

Example

SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROMCustomers);