/    /  MySQL – DISTINCT Clause

MySQL – DISTINCT Clause

 

MySQL - DISTINCT Clause

By using the MySQL DISTINCT clause, duplicate results are removed from the result set. A DISTINCT clause may only be used with a SELECT statement.

Syntax:

SELECT DISTINCT expressions
FROM tables
[WHERE conditions];

expressions – Please specify which columns or calculations you wish to retrieve.

Tables – This is the table from which you wish to retrieve records. It is required that at least one table is listed in the FROM clause.

WHERE conditions – This is optional. Conditions that must be met in order to select records.

The query will return unique values for all expressions included in the DISTINCT clause if only one expression is specified.

In the case where more than one expression is provided in the DISTINCT clause, the query will return unique combinations of the expressions listed in the clause.

In MySQL, NULL values are not ignored by the DISTINCT clause. If you use the DISTINCT clause in your SQL statement, the result set will include NULL as a distinct value.

Example

SELECT DISTINCT role FROM employee;

The following example illustrates the simplest MySQL DISTINCT clause. By using the MySQL DISTINCT clause, we can eliminate duplicate fields from the result set.

MySQL - DISTINCT Clause