題目:
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.
代碼:
1
2
|
# Write your MySQL query statement below select Email from Person group by Email having count (Email)>1 |
此題考察group by 的having以及count(列名)的用法
(1)group by 具體請看http://www.cnblogs.com/rainman/archive/2013/05/01/3053703.html(這裏講的很詳細)
(2)count(列名)是對列中不爲空的行進行計數,如果某列有n個記錄,m(m<=n)個記錄爲null,則count(該列)=n-m。