SQL SELECT DISTINCT statement

The SQL SELECT DISTINCT statement is used to retrieve unique values from a database table. The SELECT DISTINCT statement eliminates duplicates from the result set, ensuring that only one copy of each unique value is returned.

SELECT DISTINCT SYNTAX

1SELECT DISTINCT column1, column2, ... columnN 2FROM table_name;

In this syntax, column1, column2, ... columnN are the names of the columns that you want to retrieve, and table_name is the name of the table from which you want to retrieve the data.

For example, consider a table named customers that contains the following data:

idnamecity
1MaxLondon
2JaneParis
3MaxBerlin
4AliceParis

If you want to retrieve a list of unique cities, you can use the following SELECT DISTINCT statement:

1SELECT DISTINCT city 2FROM customers;

The result set of this statement will be:

city
London
Paris
Berlin

As you can see, only one copy of each unique city is returned.