【Leetcode Database】Duplicate Emails

題目:

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。















發佈了36 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章