[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.

本题是找表Person中重复的邮箱,并且该表中的邮箱字母都是小写字母。这道题比较简单,主要涉及到的知识点就是group by 与 having count 的组合,具体的解法如下:

解法一:

SELECT Email from person GROUP BY Email HAVING COUNT(Email)>1;

解法二:

  我们可以使用内交来做,用Email来内交两个表,然后返回Id不同的行,则说明两个不同的人使用了相同的邮箱:

SELECT DISTINCT p1.Email FROM person p1 
JOIN person p2 on p1.Email = p2.Email
WHERE p1.Id <> p2.Id;

猜你喜欢

转载自www.cnblogs.com/lsyb-python/p/11079416.html