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;

参考资料:

https://leetcode.com/discuss/53206/a-solution-using-a-group-by-and-another-one-using-a-self-join

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. ASP.NET中cookie与Fiter实现简单登陆,AllowAnonymous匿名登陆
  2. junit
  3. CentOS配置163的yum源
  4. Text input(文本输入框)
  5. 女友要下岗?Windows 10的Cortana何方妖孽?
  6. C#出题库项目的总结(1)
  7. 【转载】Redis多实例及分区
  8. Java中ThreadLocal的深入理解
  9. JSON漫谈
  10. tomcat gc问题总结
  11. PHPMailer发匿名邮件及Extension missing: openssl的解决
  12. PHP安全编程:跨站脚本攻击的防御(转)
  13. Spring MVC(一)
  14. 基于vue2.0前端组件库element中 el-form表单 自定义验证填坑
  15. Spark MLlib KMeans 聚类算法
  16. RTMP、HTTP-FLV、HLS,你了解常见的三大直播协议吗
  17. Android多线程的使用
  18. Win10注册表无法保存对权限所作的更改拒绝访问
  19. ARCSDE直连Oracle时出现错误Failed to connect to the specified server. Underlying DBMS error[ORA-12154: TNS:could not resolve the connect identifier specified. No extended error]
  20. 织梦替换ueditor百度编辑器,支持图片水印 教程

热门文章

  1. 2.JAVA之GUI编程布局
  2. 对C语言islower、isupper、isdigit函数的测试
  3. php内核分析(三)-全局变量
  4. Basic Tutorials of Redis(9) -First Edition RedisHelper
  5. Guid的使用
  6. 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
  7. Easyui dialog中嵌入iframe
  8. (转)配置Log4j(很详细)
  9. 批量处理sql 数据存入xml类型列
  10. 用Android Studio开发最常用到的快捷键