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.
题意:查找表中重复的Email
.
此题是很典型的对分组结果进行统计筛选例题,因此可以利用group by
进行分组,然后使用having
统计.
# Write your MySQL query statement below
SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1;
此处,对where
与group by
进行比较(引用自:https://leetcode-cn.com/problems/duplicate-emails/solution/cha-zhao-zhong-fu-de-dian-zi-you-xiang-by-he-qing-/):
where
后不能跟聚合函数,因为where
执行顺序大于聚合函数。where
子句的作用是在对查询结果进行分组前,将不符合where
条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where
条件显示特定的行。having
子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having
条件显示特定的组,也可以使用多个分组标准进行分组。