LeetCode 182. Duplicate Emails

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;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容