SQL SELECT DISTINCT
Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate values, and sometimes you may want to list only distinct (different) values.
The DISTINCT keyword is used to return only distinct (different) values.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Parameter Description:
column1, column2, ... : The names of the fields to select, which can be multiple fields. If no field names are specified, all fields will be selected.
table_name : The name of the table to query.
In this tutorial, we will use the tutorialpro sample database.
Below is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | tutorialpro.org | http://www.tutorialpro.org/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SELECT DISTINCT Example
The following SQL statement selects only distinct values from the "country" column of the "Websites" table, effectively removing duplicate values from the "country" column:
Example
SELECT DISTINCT country FROM Websites;
Output result: