Select All Records With Duplicates

Last updated on July 26, 2021 by Mike Dalisay

I wanted to select all records with duplicate session numbers (session_number) from my forms table so I’ll know if there are error of duplicate entries. I’m using MySQL Database.

Select All Records With Duplicates
Select All Records With Duplicates

Here’s a solution I found:

SELECT session_number
FROM forms
GROUP BY session_number
HAVING COUNT( session_number) > 1

Explanation:

SELECT session_number
-I’m just selecting session_number

FROM forms
-I’m selecting records from forms table.

GROUP BY session_number
-This will group all the session_number.

HAVING COUNT(session_number) > 1
-After grouping the session numbers, we will count records with that group’s session number. If it is greater than 1, it means it has duplicates, so it will be selected.

That’s it! :)

By Mike Dalisay

Hi, I'm Mike Dalisay, a coder and startup founder. Thank you for visiting codeofaninja.com! I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on Twitter, LinkedIn, Facebook, and Instagram.

Back to top