182. Duplicate Emails

来源:互联网 发布:js分页思路 编辑:程序博客网 时间:2024/06/06 09:27
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.


这道题目主要问题在于先聚合再查询。区别就在于having语句和where语句上:
[1]having与where类似,可以筛选数据,where后的表达式怎么写,having后就怎么写
[2]能用where的地方 也能用having ,用having的地方 不一定能用where(因为having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前。而 having子句在聚合后对组记录进行筛选。)
where针对表中的列发挥作用,查询数据
having是对查询结果中得到的列发挥作用,筛选数据(也就是说从查询出的结果集再次进行筛选)
select Email from Person  group by Email having count(Email)>1;



原创粉丝点击