DISTINCT

When querying data from a table, you may get duplicate rows. In order to remove these duplicate rows, you use the DISTINCT clause in the SELECT statement.

Here is the syntax of the DISTINCT clause:

SELECT DISTINCT
    select_list
FROM
    table_name;

MySQL DISTINCT examples

Let’s take a look at a simple example of using the DISTINCT clause to select the unique last names of employees from the employeestable.

First, query the last names of employees from the employees table using the following SELECT statement:

SELECT 
    lastname
FROM
    employees
ORDER BY 
    lastname;

As clearly shown in the output, some employees have the same last name e.g, Bondur,Firrelli .

This statement uses the DISTINCT clause to select unique last names from the employees table:

SELECT 
    DISTINCT lastname
FROM
    employees
ORDER BY 
    lastname;

As you can see from the output, duplicate last names has been eliminated in the result set.

Last updated