Leetcode 182. Duplicate Emails

来源:互联网 发布:好考试软件 编辑:程序博客网 时间:2024/06/06 16:28

182. Duplicate Emails

Total Accepted: 23531 Total Submissions: 63036 Difficulty: Easy

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 |+---------+

思路:

返回的是重复的email,也就是count大于1的email。

# Write your MySQL query statement belowSELECT Email from PersonGroup By EmailHaving Count(*) > 1;

这里借用参考文章一段话:

如果没有 GROUP BY 那么查询语句是 SELECT COUNT(Email), Email FROM Person,其结果是:COUNT(Email)Email3a@b.com因为聚合函数 COUNT 只会返回一行,所以结果只有一行。如果需要每组的 Email 个数,则需要 GROUP BY 先分组,而事实上使用聚合函数一般都会 GROUP BY。


0 0