Select All Records With Duplicates

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! :)

Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.