Menu Close

MySQL SELECT DISTINCT Statement

In this article, you will learn everything about MySQL SELECT DISTINCT statement to eliminate the duplicate rows from the result set.
DISTINCT Keyword in MySQL is one of the most important keywords to select only unique values from the result set.

MySQL SELECT DISTINCT Statement Introduction

The MySQL SELECT DISTINCT statement allows us the select only distinct ( different ) values.

Most of the time. Inside a table, a column contains many duplicate values and you want to select only unique values from the table, in that situation you can go with MySQL SELECT DISTINCT statement.

To select only unique from the table, you have to use the DISTINCT keyword after the SELECT keyword.

The following syntax of the example of MySQL SELECT DISTINCT statement.

Syntax

This is the basic syntax of distinct keywords in MySQL.

SELECT DISTINCT select_list FROM table_name;

Let’s break down the above syntax step by step.

  • The SELECT keyword is a reversed word that is used to select columns from the table.
  • DISTINCT clause evaluates the uniqueness of the row based on the values of the columns.
  • select_list represents the columns list you want to select.
  • table_name represents the name of the table, you want to select columns from.

MySQL SELECT DISTINCT Examples:

Here we will use students table to use the DISTINCT Clause.

First, we will select last_name from the students table using MySQL SELECT statement.

Example

SELECT last_name FROM students;

Output


+-----------+
| last_name |
+-----------+
| Rao       |
| Doe       |
| Kumar     |
| Singh     |
| Kaur      |
| Garima    |
| Kumar     |
| Singh     |
+-----------+
8 rows in set (0.00 sec)

As you can see in the above output Kumar and Singh appears more than one time, which is duplicate.

Now, we will use the MySQL SELECT DISTINCT statement to eliminate the duplicate rows from the result set.

Example: MySQL SELECT DISTINCT On One Column

SELECT DISTINCT last_name FROM students;

Output


+-----------+
| last_name |
+-----------+
| Rao       |
| Doe       |
| Kumar     |
| Singh     |
| Kaur      |
| Garima    |
+-----------+
6 rows in set (0.07 sec)

MySQL DISTINCT with multiple columns

Firstly, we will select last_name and course from the students table without using the DISTINCT keyword.

Example:

SELECT last_name, course FROM students;

Output


+-----------+--------+
| last_name | course |
+-----------+--------+
| Rao       | MCA    |
| Doe       | Mtech  |
| Kumar     | B.A.   |
| Singh     | Btech  |
| Kaur      | LLB    |
| Garima    | BCA    |
| Kumar     | MCA    |
| Singh     | Btech  |
+-----------+--------+
8 rows in set (0.00 sec)

As you can see in the above output, last_name and course columns contain duplicate data. Now we will use the DISTINCT statement to eliminate the duplicate rows.

Example: MySQL SELECT DISTINCT On Multiple Column

SELECT DISTINCT last_name, course FROM students;

Output


+-----------+--------+
| last_name | course |
+-----------+--------+
| Rao       | MCA    |
| Doe       | Mtech  |
| Kumar     | B.A.   |
| Singh     | Btech  |
| Kaur      | LLB    |
| Garima    | BCA    |
| Kumar     | MCA    |
+-----------+--------+
7 rows in set (0.00 sec)

Summary

So, in this article, you have seen all about MySQL SELECT DISTINCT statement to select only unique records from the result or eliminate the duplicate rows from the result set.DISTINCT keyword on of the most useful clause, especially when you want to select only unique records from the table.

I hope this MySQL SELECT DISTINCT statement tutorial is helpful for you. If you like this article, please share and keep visiting for further MySQL tutorials.

Ref:- Click Here

Thanks for your valuable timeā€¦..

Previous Article
Next Article

Related Posts