Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL SELECT DISTINCT Statement

SQL SELECT DISTINCT Statement


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

In a table, a column may contain multiple duplicate values, and sometimes you may want to list only the distinct 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:


In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:


+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | Runoob Tutorial      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | 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, that is, it removes duplicate values from the "country" column:

Example

SELECT

DISTINCT

country

FROM

Websites

;

Output result: