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函數(shù)結(jié)合group by可以統(tǒng)計(jì)指定字段的數(shù)量。假如重復(fù)的話,count函數(shù)肯定大于1.
2.當(dāng)進(jìn)過第一步后,再從第一步后的臨時(shí)表中篩選count >1的字段記錄.
結(jié)果:
# 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;