LetCode-MSSQL查找重复的电子邮箱

image.png
sql的题目如下所示,查询出重复的电子邮箱

解法(1):查询出查询出Email相等 Id不相同的数据具体语句如下所示:

select  a.Email from Person as a,Person as b where a.Email=b.Email and a.Id!=b.Id

image.png
此时我们可以看到我们的语句中输出了2次结果但是预期结果只输出了1次
此时我们只需加上去重就ok了去重的话我们可以使用 distinct或者group by
具体语句如下所示:

select  a.Email from Person as a,Person as b where a.Email=b.Email and a.Id!=b.Id group by a.Email
select  distinct a.Email from Person as a,Person as b where a.Email=b.Email and a.Id!=b.Id 

执行后 使用group by 874ms 使用distinct 728 ms

解法(2) 直接使用group by 去重并用 having count 筛选 Email重复的项目
代码如下所示:

select Email from Person group by Email having count(Email) > 1;
posted @ 2020-04-03 17:29  Bluegoing  阅读(1)  评论(0编辑  收藏  举报  来源