1. 程式人生 > >LeetCode-查詢重複的電子郵箱(duplicate-emails)

LeetCode-查詢重複的電子郵箱(duplicate-emails)

題目難度: 簡單

編寫一個 SQL 查詢,查詢 Person 表中所有重複的電子郵箱。

示例:

+----+---------+
| Id | Email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+

根據以上輸入,你的查詢應返回以下結果:

+---------+
| Email   |
+---------+
| [email protected] |
+---------+

**說明:**所有電子郵箱都是小寫字母。

Solution

Language: MySQL

# 1st
SELECT Email FROM Person GROUP BY Email
HAVING COUNT(*) > 1;
​
# 2nd
SELECT DISTINCT p1.Email FROM Person p1
JOIN Person p2 ON p1.Email = p2.Email
WHERE p1.Id <> p2.Id;
​