[LeetCode] Duplicate Emails 重复的邮箱

简介:

Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

Note: All emails are in lowercase.

这道题让我们求重复的邮箱,那么最直接的方法就是用Group by...Having Count(*)...的固定搭配来做,代码如下:

解法一:

SELECT Email FROM Person GROUP BY Email
HAVING COUNT(*) > 1;

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

解法二:

SELECT DISTINCT p1.Email FROM Person p1
JOIN Person p2 ON p1.Email = p2.Email
WHERE p1.Id <> p2.Id;

本文转自博客园Grandyang的博客,原文链接:重复的邮箱[LeetCode] Duplicate Emails ,如需转载请自行联系原博主。

相关文章
|
算法 Python
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 217. 存在重复元素 Contains Duplicate
LeetCode 219: 存在重复元素 II Contains Duplicate II
题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。 ​ Given an array of integers and an integer k, find o...
602 0
LeetCode 217:存在重复元素 Contains Duplicate
题目: 给定一个整数数组,判断是否存在重复元素。 Given an array of integers, find if the array contains any duplicates. 如果任何值在数组中出现至少两次,函数返回 true。
9575 0
|
存储 索引
LeetCode 219 Contains Duplicate II(包含重复数字2)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50593169 翻译 给定一个整型数组和一个整型数K, 找出是否存在两个不同的索引i和j,使得nums[i] = nums[j], 并且它们之间的距离最大为K。
1019 0
|
测试技术
LeetCode 217 Contains Duplicate(包含重复数字)(Vector、hash)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504102 翻译 给定一个整型数字数组,找出这个数组是否包含任何重复内容。
1059 0