How is it possible to filter out the duplications in the rows of result set return by MySQL?



It can be possible by using the DISTINCT keyword in SELECT clause. The DISTINCT applies to the combination of all data fields specified in SELECT clause.

Example

We have the table ‘Student’ on which we have applied DISTINCT keyword as follows −

mysql> Select * from student;

+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 1    | Gaurav  | Delhi   | Computers |
| 2    | Aarav   | Mumbai  | History   |
| 15   | Harshit | Delhi   | Commerce  |
| 17   | Raman   | Shimla  | Computers |
| 20   | Gaurav  | Jaipur  | History   |
+------+---------+---------+-----------+

5 rows in set (0.00 sec)

mysql> Select DISTINCT Address from student;

+---------+
| Address |
+---------+
| Delhi   |
| Mumbai  |
| Shimla  |
| Jaipur  |
+---------+

4 rows in set (0.00 sec)

mysql> Select DISTINCT * from student;

+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 1    | Gaurav  | Delhi   | Computers |
| 2    | Aarav   | Mumbai  | History   |
| 15   | Harshit | Delhi   | Commerce  |
| 17   | Raman   | Shimla  | Computers |
| 20   | Gaurav  | Jaipur  | History   |
+------+---------+---------+-----------+

5 rows in set (0.00 sec)

mysql> Select DISTINCT name from student;

+---------+
| name    |
+---------+
| Gaurav  |
| Aarav   |
| Harshit |
| Raman   |
+---------+

4 rows in set (0.00 sec)

Advertisements