Duplicate Emails
Question
Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| [email protected] |
+---------+
Note: All emails are in lowercase.
Thought
In order to check the count for duplicated emails, it is necessary to split the table by Email with GROUP BY
. The approach of filtering records for GROUP BY
is HAVING
clause, which is very similar to WHERE
except that HAVING
only works for the groups generated by GROUP BY
.
Code
-- Write your MySQL query statement below
SELECT Email
FROM Person
GROUP BY Email
HAVING COUNT(*) > 1;