Get All Unique Rows in MySQL Result Set



With the help of DISTINCT keyword in SELECT statement, we can get the unique rows in MySQL result set.

Example

mysql> Select * from names;

+------+-----------+
| id   | name      |
+------+-----------+
| 1    | Rahul     |
| 2    | Gaurav    |
| 3    | Raman     |
| 4    | Aarav     |
| 5    | Ram       |
| 5    | Ram       |
| 5    | Ram       |
+------+-----------+

7 rows in set (0.00 sec)

As we can see that table ‘names’ is having three duplicate rows, with the help of following query we can get the result set having only unique rows.

mysql> Select DISTINCT * from names;

+------+-----------+
| id   | name      |
+------+-----------+
| 1    | Rahul     |
| 2    | Gaurav    |
| 3    | Raman     |
| 4    | Aarav     |
| 5    | Ram       |
+------+-----------+

5 rows in set (0.00 sec)
Updated on: 2020-06-20T07:09:52+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements