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! 🙂


Comments

2 responses to “Select All Records With Duplicates”

  1. Anonymous Avatar
    Anonymous

    First you write:
    SELECT session_number

    Then under explanation you write:
    SELECT DISTINCT session_number

    Which is correct?

  2. oh, thanks for the catch, i think doing just SELECT session_number is correct

Leave a Reply

Your email address will not be published. Required fields are marked *