- 学习:知识的初次邂逅
- 复习:知识的温故知新
- 练习:知识的实践应用
目录
一,原题力扣链接
二,题干
表:
Person
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id 是该表的主键(具有唯一值的列)。 此表的每一行都包含一封电子邮件。电子邮件不包含大写字母。编写解决方案来报告所有重复的电子邮件。 请注意,可以保证电子邮件字段不为 NULL。
以 任意顺序 返回结果表。
结果格式如下例。
示例 1:
输入: Person 表: +----+---------+ | id | email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+ 输出: +---------+ | Email | +---------+ | a@b.com | +---------+ 解释: a@b.com 出现了两次。
三,建表语句
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');
四,分析
题解:
表:人类表
字段:id,邮箱
要求。查找重复的邮箱,id是主键,邮箱一定不为空
思路:
简单的分组+聚合+过滤
解法一:以邮箱分组,聚合count(id)的次数 ,过滤count(id)>1
解法二:开窗
第一步,count开窗 以email分组 count id
第二步,where 开窗的值 大于1
最后distinct 邮箱字段
五,SQL解答
解法一,分组+聚合+过滤
select email from person group by email having count(id)>1;
解法二:开窗
with t1 as (
select id,email,count(id) over (partition by email) co
from person
)
select distinct email from t1 where t1.co>1;
六,验证
七,知识点总结
- 分组+聚合+过滤的运用
- 开窗的运用
- 去重的运用
- 学习:知识的初次邂逅
- 复习:知识的温故知新
- 练习:知识的实践应用