182. 查找重复的电子邮箱

难度系数: 简单
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
返回的结果表中的数据可以按任意顺序排列。

SQL结构

1
2
3
4
5
Create table If Not Exists Person (id int, email varchar(255))
Truncate table Person
insert into Person (id, email) values ('1', 'a@b.com')
insert into Person (id, email) values ('2', 'c@d.com')
insert into Person (id, email) values ('3', 'a@b.com')

表:Person

1
2
3
4
5
6
7
8
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id 是这个表的主键。
表的每一行都包含一个邮箱地址。邮箱地址不包含大写字母。

示例 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
输入:
Person 表:
+----+---------+
| id | email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
输出:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
解释:a@b.com重复了两次

解法1:

1
2
3
4
SELECT Email
FROM Person
GROUP BY Email
HAVING count(*)>1

解法2:

1
2
3
4
SELECT DISTINCT a.Email
FROM Person AS a, Person AS b
WHERE a.id != b.id
AND a.Email=b.Email

原题链接:https://leetcode.cn/problems/duplicate-emails