LeetCode DB: 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.

考察having的使用having的对象是group by中的字段或者是select中使用了聚集函数的字段(当然要对应用了聚集函数的字段取个别名,因为已经不是原先的意义了),如果取了别名可以在having中直接使用该别名如果下面的ECount这个别名,如果没有取那么要把select中的聚集函数表达式再写一次。不过以前用SQLServer的时候好像不能直接用别名,记不清了。

# Write your MySQL query statement below
select Email from (select Email, count(*) as ECount from Person group by Email having ECount > 1) T
posted @ 2015-08-03 22:42  卖程序的小歪  阅读(121)  评论(0编辑  收藏  举报