How to find duplicate rows of a table in sql

December 10, 2009    duplicate rows mysql db sql queries

This is a nice quick easy way to find duplicate rows in your database based on one field.
So say for instance id like to find all users with duplicate rows in my orders table.

The following query can achieve that.

select userid, count(*) from orders group by userid HAVING count(*) > 1

//this will fetch all the userids with duplicate rows, then you could run a delete query or whatever it is you need to do to get rid of them in a while loop of this.

blog comments powered by Disqus