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

By Mike Dalisay

I'm Mike Dalisay, a pro web developer since 2010. I love web development. Improving our tutorials and source codes makes me happy. Do you want to suggest an edit to our tutorial? Got something to ask about our source codes? You may use our comments section below or email our team at [email protected]

2 comments

  1. First you write:
    SELECT session_number

    Then under explanation you write:
    SELECT DISTINCT session_number

    Which is correct?

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top