How can we get all the 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: 20-Jun-2020

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements