202553-sql

目录

[一、196. 删除重复的电子邮箱 - 力扣(LeetCode)](#一、196. 删除重复的电子邮箱 - 力扣(LeetCode))

[二、602. 好友申请 II :谁有最多的好友 - 力扣(LeetCode)](#二、602. 好友申请 II :谁有最多的好友 - 力扣(LeetCode))

[三、176. 第二高的薪水 - 力扣(LeetCode)](#三、176. 第二高的薪水 - 力扣(LeetCode))


一、196. 删除重复的电子邮箱 - 力扣(LeetCode)

题意就是删除删除重复的邮箱

很容易可以想到 delete from person where id in (一坨)

绕了个弯子 让你写删除语句本质还是写查询语句

  1. 第一层查询使用窗口函数 分组加排序

    sql 复制代码
    select *,
    row_number() over(partition by email order by id asc) as 'rank' 
    from Person
  2. 可以显然得出 临时表中rank >1 的都是重复的,嵌套一层查id出来

    sql 复制代码
     select id from (
            select *,
                    row_number() over(partition by email order by id asc) as 'rank' from Person
        ) temp  where temp.rank = 1
  3. 执行删除语句

    sql 复制代码
    delete from Person where id not in (
        select id from (
            select *,row_number() over(partition by email order by id asc) as 'rank' from Person
        ) temp  where temp.rank = 1
    )

二、602. 好友申请 II :谁有最多的好友 - 力扣(LeetCode)

sql 复制代码
with t1 as(
    select requester_id as 'id' from RequestAccepted
    union all
    select accepter_id  as 'id' from RequestAccepted
),
t2 as(
    select id,count(id) over(partition by id rows between unbounded preceding and unbounded following) as 'num'
    from t1
),
t3 as(
    select *,dense_rank() over(partition by null order by num desc) as 'rank'
    from t2
)
select id,num 
from t3 
where t3.rank = 1
limit 1

理解就是加好友是相互的!!!!!!!!

把两列数据并成一列 然后窗口函数分组排序

三、176. 第二高的薪水 - 力扣(LeetCode)

也是窗口函数分组排序 但是这个题就比较麻烦 需要考虑空结果集输出null

sql 复制代码
select ifnull((
    with t1 as(
        select *,dense_rank() over(partition by null order by salary desc) as 'rank'
        from Employee
    ),
    t2 as(
        select distinct salary as 'SecondHighestSalary' 
        from t1
        where t1.rank = 2
    )
    select SecondHighestSalary from t2
),null) as 'SecondHighestSalary'

结束三道sql!

相关推荐
姚远Oracle ACE1 小时前
解读Oracle AWR报告:Global Cache and Enqueue Services - Workload Characteristics
数据库·oracle
流星白龙1 小时前
【Qt】7.信号和槽_connect函数用法(2)
java·数据库·qt
Zzz 小生3 小时前
Claude Code学习笔记(四)-助你快速搭建首个Python项目
大数据·数据库·elasticsearch
nongcunqq6 小时前
abap 操作 excel
java·数据库·excel
rain bye bye7 小时前
calibre LVS 跑不起来 就将setup 的LVS Option connect下的 connect all nets by name 打开。
服务器·数据库·lvs
阿里云大数据AI技术8 小时前
云栖实录|MaxCompute全新升级:AI时代的原生数据仓库
大数据·数据库·云原生
不剪发的Tony老师9 小时前
Valentina Studio:一款跨平台的数据库管理工具
数据库·sql
重生之我要当java大帝9 小时前
java微服务-尚医通-编写医院设置接口下
java·开发语言·sql
weixin_307779139 小时前
在 Microsoft Azure 上部署 ClickHouse 数据仓库:托管服务与自行部署的全面指南
开发语言·数据库·数据仓库·云计算·azure
六元七角八分9 小时前
pom.xml
xml·数据库