Press "Enter" to skip to content

mysql-distinct

MySQL DISTINCT clause

MYSQL Distinct remove duplicate row from the table when you use with a select clause

The DISTINCT clause in MySQL is used to eliminate duplicate rows from the result set of a query. It operates on the unique combination of values in all columns of the query.

Here is the basic syntax for using the DISTINCT clause in a MySQL query:

SELECT DISTINCT column1, column2, ...
FROM table_name;

In the above example, the DISTINCT keyword is used to specify that only unique rows should be returned. The column1, column2, ... specifies the columns that you want to retrieve data from. The FROM clause specifies the name of the table that you want to retrieve data from.

Note that when you use the DISTINCT clause, the order of the rows in the result set is indeterminate. If you want to retrieve the rows in a specific order, you can use the ORDER BY clause.

Table structure Used for Example explanation

For the example country table is used .The structure of country table is as follow:
+—————-+————+
| Field | Type |
+—————-+————+
| Code | char(3) |
| Name | char(52) |
| Continent | char(40) |
| Region | char(26) |
| SurfaceArea | float(10,2)|
| IndepYear | smallint(6)|
| Population | int(11) |
| LifeExpectancy | float(3,1) |
| GNP | float(10,2)|
| GNPOld | float(10,2)|
| LocalName | char(45) |
| GovernmentForm | char(45) |
| HeadOfState | char(60) |
| Capital | int(11) |
| Code2 | char(2) |
+—————-+————+

Example for MySQL DISTINCT clause

Select Distinct continent from country ;

Output of Above Example

mysql> Select Distinct continent from country ;
+—————+
| continent |
+—————+
| Asia |
| Europe |
| North America |
| Africa |
| Oceania |
| South America |
| Antarctica |
+—————+
7 rows in set (0.07 sec)