MySQL Distinct

The MySQL Select Distinct Statement is used to retrieve unique rows (by removing the duplicate rows) from the specified column in the SELECT Statement.

MySQL Select Distinct Syntax

The basic syntax of the Select Distinct in MySQL can write as:

SELECT DISTINCT [Columns]
FROM Source
WHERE Conditions -- This is Optional
  • DISTINCT: This keyword returns Unique Columns
  • Columns: It allows us to choose the number of columns you want to use from the table. It may be one or more.
  • Source: It can be one or more tables in a Database. Use Joins to combine multiple tables.

We are going to use the below shown data to explain the Select Distinct in MySQL with example.

Customers Table Records 1

SQL DISTINCT on Single Column

In this MySQL Distinct Single column example, We are going to select the unique records from the Education Column. Before we get unique records, let me select all the records from the education column.

USE company;
SELECT Education
FROM customerdetails;
MySQL Select Distinct Column 2

Now, let me use this keyword in the SELECT Statement.

USE company;
SELECT DISTINCT Education
FROM customerdetails;
MySQL Select Distinct 3

MySQL Distinct on Multiple Columns

When we use MySQL Distinct on multiple columns, then the SELECT Statement writes the unique combination of multiple columns instead of unique individual records. In this MySQL example, We are going to select the unique records present in the Education Column, and Profession Column.

USE company;
SELECT DISTINCT Education, Profession
FROM customerdetails
ORDER BY Education, Profession;
MySQL Distinct on Multiple Columns 4

Although we used the MySQL Distinct Keyword in the SELECT Statement. From the above screenshot you can see, it is returning duplicates because

  • Bachelors and Developer is a unique Combination
  • Bachelors and Programming is a unique Combination, etc.

MySQL Distinct Where Clause Example

In this example, we show you, How to use this statement along with WHERE Clause. The following statement returns the unique Education, Profession from customers table whose Yearly Income is greater than or equal to 85000.

USE company;
SELECT DISTINCT Education, Profession
FROM customerdetails
WHERE Yearly_Income > 85000;
MySQL Distinct Where Clause 5

Although there are 13 unique records for the Education, professional Column combination. 10 records are not matching the condition in Where clause. That’s why the screenshot below is showing 5 records.

NOTE: It considers the NULL records as a valid unique record. So, Please use any Not Null function to remove NULLS.

MySQL Distinct Vs Group By

The MySQL Select Distinct behavior inherited from the Group By. If you use the Group By Clause without any Aggregate Function, then it acts as the Distinct keyword. And the only difference between them is:

  • Group By sort the Data first, and then perform grouping.
  • This keyword does not perform any sorting.

If you use this Keyword along with the Order By then, it provides the same result as Group By. The following statement returns the unique Profession values from customer details

USE company;
SELECT DISTINCT Profession 
FROM customerdetails;
MySQL Distinct 6

Let me remove the keyword, and use the Group By clause:

USE company;
SELECT Profession 
FROM customerdetails
GROUP BY Profession;

As you can see they are returning the same result but not in the same order

MySQL Distinct 7

This time, I used this Keyword along with the Order By

USE company;
SELECT DISTINCT Profession 
FROM customerdetails
ORDER BY Profession ASC;

As you can see, it is returning same result as Group By

MySQL Distinct Group By 8

Select Unique Rows in Command Prompt Example

Let me show you how to select distinct records using the MySQL command prompt. Here we are selecting the distinct Education, and profession records from customer details column.

USE company;
SELECT DISTINCT Education, Profession 
FROM customerdetails
ORDER BY Education ASC, Profession ASC;
MySQL Distinct 9