Write a SQL query to find all duplicate emails in a table named Person.
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
For example, your query should return the following for the above table:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
Note: All emails are in lowercase.
分析:
1.用count函数结合group by可以统计指定字段的数量。假如重复的话,count函数肯定大于1.
2.当进过第一步后,再从第一步后的临时表中筛选count >1的字段记录.
结果:
# Write your MySQL query statement below
SELECT Email
from(
SELECT Email, COUNT(Email) as count
FROM Person GROUP BY Email
) t
where t.count > 1;