Saturday, 18 July 2020

How to select all duplicates in MySQL

Select all duplicate rows :
first craete table
table name : demotable
column : Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
       Name varchar (100)

demo data :

demodata

+----+--------+
| Id | Name   |
+----+--------+
| 1  | John   |
| 2  | Chris  |
| 3  | John   |
| 4  | David  |
| 5  | Bob    |
| 6  | Chris  |
| 7  | Mike   |
| 8  | Robert |
| 9  | Mike   |
+----+--------+


select *from prersi where open in (select open from prersi group by open having count(*) > 1) 


Output

+----+-------+
| Id | Name  |
+----+-------+
| 1  | John  |
| 2  | Chris |
| 3  | John  |
| 6  | Chris |
| 7  | Mike  |
| 9  | Mike  |
+----+-------+