MySQL Distinct Clause

MySQL Distinct clause is used to remove duplicate values from the result dataset.

MySQL Distinct syntax

The Basic Syntax of the MySQL Distinct is as follows

SELECT DISTINCT <column-names>
FROM <table_name>;

MySQL Distinct Clause for a single column in MySQL

To get the distinct values from the data for a single column, we do:

select distinct rating from film;

Distinct Clause for Multiple columns in MySQL

When we use distinct with multiple columns, MySQL will return all the records in the dataset that are a unique combination of both the columns.

select distinct rating,title from film
order by rating desc;

Distinct clause with NULL values in MySQL

When we try to get all the distinct values in a column that contains NULL values, we get a single row with NULL and the rest of the unique values. NULL values are not repeated in the result set.

select distinct city_id from address;