LeetCode: Duplicate Emails

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


將自身連接,等值項爲email,然後尋找id不同email相同的元組。
第一次寫這種題,數據庫還真難。看來還有很長的路要走。


# Write your MySQL query statement below
select distinct C.AEmail as Email
from
(select A.Id as AId,A.Email as AEmail,B.Id as BId,B.Email as BEmail
from Person as A,Person as B
where A.Email=B.Email) as C
where C.AId<>C.BId    
and C.AEmail=C.BEmail;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章