English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to remove duplicate values from the result set.
When retrieving data from a database table, the result set may contain duplicate rows or values. If you want to remove these duplicate values, you can specify the keyword DISTINCT directly after the SELECT keyword, as shown below:
The DISTINCT clause is used to remove duplicate rows from the result set:
SELECT DISTINCT column_list FROM table_name;
Here,column_listis a list of database table column names or field names separated by commas (for examplename,age,countryetc.).
Note: The behavior of the DISTINCT clause is similar toUNIQUEconstraint, except for how it treats nulls. Two NULL values are considered unique, and yet they are not considered different from each other.
Let's look at some examples to demonstrate how it actually works.
Suppose we have acustomerstable, which contains the following records:
+---------+--------------------+-----------+-------------+ |cust_id|cust_name |city |postal_code | +---------+--------------------+-----------+-------------+ | 1 |Maria Anders |Berlin | 12209 | | 2 |Fran Wilson |Madrid | 28023 | | 3 |Dominique Perrier |Paris | 75016 | | 4 |Martin Blank |Turin | 10100 | | 5 |Thomas Hardy |Portland | 97219 | | 6 |Christina Aguilera|Madrid | 28001 | +---------+--------------------+-----------+-------------+
Now execute the following statement, which returns thecityAll rows in the column.
SELECT city FROM customers;
After executing, you will get the following output:
+-----------+ | city | +-----------+ | Berlin | | Madrid | | Paris | | Turin | | Portland | | Madrid | +-----------+
If you take a close look at the output, you will find that the city 'Madrid' appears twice in our results. That's not good, is it? Alright, let's solve this problem.
The following statement is used for DISTINCT incustomersList of all cities generated in the table.
SELECT DISTINCT city FROM customers;
After executing the above command, you will get the following output:
+-----------+ | city | +-----------+ | Berlin | | Madrid | | Paris | | Turin | | Portland | +-----------+
As you can see, there are no duplicate values in this result set.
Note:If the SELECT DISTINCT statement is used on a column with multiple NULL values, SQL retains one NULL value and removes other values from the result set because DISTINCT treats all NULL values as the same value.