Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all users with duplicate email addresses

Tags:

sql

mysql

I am writing a MySQL query where I can get all records from the user table with duplicate email addresses.

This is the query I have so far.. my PHPMyAdmin keeps loading:

select personal_email, userid
from user
where personal_email in (
select personal_email
from user
group by personal_email
having count(personal_email) > 1);

If I use the query below I get all double emails just once:

select personal_email, count(personal_email)
from user
group by personal_email
having count(personal_email) > 1

The goal is getting all the records with duplicate emails.

like image 256
Patrick Leijser Avatar asked Sep 18 '13 14:09

Patrick Leijser


People also ask

Can Mailchimp detect duplicate emails?

Mailchimp automatically scans for duplicates when you add or import contacts to a single audience. If an address is found in an import file twice or more, we'll only add it once. If you try to add someone who's already in your audience, we'll prevent it to avoid duplication.

What does duplicate user email mean?

Duplicates of the same message will occur if your email account is configured to forward email to multiple addresses. For example, the original may arrive in your business acount with a copy forwarded to your home account. They will both arrive in the same inbox if the same mail client is checking both addresses.


2 Answers

Try the query with a JOIN instead of IN:

SELECT  user.personal_email, user.userid
FROM    user 
        INNER JOIN
        (   SELECT  personal_email
            FROM    User
            GROUP BY personal_email
            HAVING COUNT(*) > 1
        ) dupe
            ON dupe.personal_email = user.personal_email;

MySQL often optimises INNER JOINs much better.

like image 114
GarethD Avatar answered Sep 18 '22 17:09

GarethD


Perhaps the most efficient way to do this in MySQL is to use an exists clause:

select u.*
from user u
where exists (select 1
              from user u2
              where u.personal_email = u2.personal_email and
                    u.userid <> u2.userid
             );

For optimal performance, create an index on user(personal_email, userid).

like image 29
Gordon Linoff Avatar answered Sep 21 '22 17:09

Gordon Linoff