当前位置: 首页 > news >正文

【LeetCode高频SQL50题-基础版】打卡第9天:第46~50题

文章目录

  • 【LeetCode高频SQL50题-基础版】打卡第9天:第46~50题
    • ⛅前言
    • 患某种疾病的患者
      • 🔒题目
      • 🔑题解
    • 第二高的薪水
      • 🔒题目
      • 🔑题解
    • 按日期分组销售产品
      • 🔒题目
      • 🔑题解
    • 列出指定时间段内所有的下单产品
      • 🔒题目
      • 🔑题解
    • 查找拥有有效邮箱的用户
      • 🔒题目
      • 🔑题解

【LeetCode高频SQL50题-基础版】打卡第9天:第46~50题

⛅前言

  在这个博客专栏中,我将为大家提供关于 LeetCode 高频 SQL 题目的基础版解析。LeetCode 是一个非常受欢迎的编程练习平台,其中的 SQL 题目涵盖了各种常见的数据库操作和查询任务。对于计算机科班出身的同学来说,SQL 是一个基础而又重要的技能。不仅在面试过程中经常会遇到 SQL 相关的考题,而且在日常的开发工作中,掌握 SQL 的能力也是必备的。

  本专栏的目的是帮助读者掌握 LeetCode 上的高频 SQL 题目,并提供对每个题目的解析和解决方案。我们将重点关注那些经常出现在面试中的题目,并提供一个基础版的解法,让读者更好地理解问题的本质和解题思路。无论你是准备找工作还是提升自己的技能,在这个专栏中,你可以学习到很多关于 SQL 的实践经验和技巧,从而更加深入地理解数据库的操作和优化。

  我希望通过这个专栏的分享,能够帮助读者在 SQL 的领域里取得更好的成绩和进步。如果你对这个话题感兴趣,那么就跟随我一起,开始我们的 LeetCode 高频 SQL 之旅吧!

  • 博客主页💖:知识汲取者的博客
  • LeetCode高频SQL100题专栏🚀:LeetCode高频SQL100题_知识汲取者的博客-CSDN博客
  • Gitee地址📁:知识汲取者 (aghp) - Gitee.com
  • 题目来源📢:高频 SQL 50 题(基础版) - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台

患某种疾病的患者

🔒题目

题目来源:1527.患某种疾病的患者

image-20231014173531804

🔑题解

  • 考察知识点
select min(id), email
from Person
group by email;

PS:时隔两天又遇到 LeetCode 的Bug了,我在本地运行这个SQL可以过,但是在LeetCode运行无法通过,测试数据居然 group by都没有进行去重,上次也遇到这个LeetCode的Bug了,害我还思考了很久,这次我留心了

结果发现是我题目没看清楚🤣,题目要求是删除,我直接下意识搞一个查询去了😑

正确的SQL:

delete from Person
where id not in (select min(id)from Persongroup by email
);

结果报错:You can't specify target table 'Person' for update in FROM clause

哎呀😟查询SQL写多了,删除SQL不会写了😑

这个报错的原因是:MySQL不允许在子查询中直接引用待更新的目标表导致的,所以需要使用自连接

delete p from Person p
left join (select min(id) idfrom Persongroup by email
) t 
on p.id = t.id
where t.id is null;

还有一种更见简洁的方式,使用自连接:一旦判断两条记录的邮箱相同,直接删除id较大的那条记录

delete p1 from Person p1, Person p2
where p1.email = p2.email and p1.id > p2.id;

PS:第一种写法的性能要更加高,因为第一条SQL是作笛卡尔积,然后进行过滤,第二条SQL会遍历两张表的每一条记录

第二高的薪水

🔒题目

题目来源:176.第二高的薪水

image-20231014180648548

🔑题解

  • 考察知识点

分析:直接可以使用窗口函数,一下就成功解决了

方式一:使用dense_rank

dese_rank按照指定分组进行排序,不会跳过重复的序号(1,1,2)

select salary SecondHighestSalary
from(select salary, dense_rank() over(order by salary desc) rankingfrom Employee
) t
where ranking = 2;

发现无法处理数据不足两条的情况,数组不足两条预期结果是null,但是实际查询得到的结果是什么都没有

select if(max(ranking) < 2, null, salary) SecondHighestSalary
from(select salary, dense_rank() over(order by salary desc) rankingfrom Employee
) t
where ranking = 2;

方式二:使用nth_value

nth_value用于获取期望值的第n个

1)

select *, nth_value(salary, 2) over(order by salary desc) SecondHighestSalary 
from Employee;
| id | salary | SecondHighestSalary |
| -- | ------ | ------------- |
| 3  | 300    | null          |
| 2  | 200    | 200           |
| 1  | 100    | 200           |

2)

select max(SecondHighestSalary) SecondHighestSalary
from (select nth_value(salary, 2) over(partition by salary order by salary desc) SecondHighestSalary from Employee
) t;

结果发现对于结果集中只有一种薪资时无法获取正确的结果,比如表中薪资 都是1000时,此时结果居然是1000,预期结果是null

方式三:使用普通函数

窗口函数只能在MySQL8或者更高的版本中使用,所以这里我们还算有必要学习如何使用普通函数来解决这一题


按日期分组销售产品

🔒题目

题目来源:1484.按日期分组销售产品

image-20231015233320137

🔑题解

  • 考察知识点group bycountdistinctorder bygroup_concat
    • group_concat(column order by column seprartop char):将多行记录的某一个字段按指定分隔符合并为单个字符串

分析:这一题的难点在于筛选出 products ,只要这个会了,其他的都很简单

1)

select sell_date, count(distinct product) num_sold
from Activities
group by sell_date
| sell_date  | num_sold |
| ---------- | -------- |
| 2020-05-30 | 3        |
| 2020-06-01 | 2        |
| 2020-06-02 | 1        |

2)

select sell_date,count(distinct product) num_sold,group_concat(distinct product order by product separator ',') products
from Activities
group by sell_date
order by sell_date asc

列出指定时间段内所有的下单产品

🔒题目

题目来源:1327.列出指定时间段内所有的下单产品

image-20231015234451276

🔑题解

  • 考察知识点group byhaving连接
select p.product_name, sum(o.unit) unit
from Products p join Orders o on p.product_id = o.product_id
where year(o.order_date) = 2020 and month(o.order_date) = 2
group by p.product_id
having sum(o.unit) >= 100

这里还提供一种方法:

select p.product_name, sum(o.unit) unit
from Products p join Orders o on p.product_id = o.product_id
where o.order_date like '2020-02%'
group by p.product_id
having sum(o.unit) >= 100

查找拥有有效邮箱的用户

🔒题目

题目来源:1517.查找拥有有效邮箱的用户

image-20231015234552215

🔑题解

  • 考察知识点正则表达式

遇到这种字符串匹配,很容易想到使用正则表达式

select user_id, name, mail
from Users
where mail regexp '^[a-zA-Z][a-zA-Z0-9_.-]*\\@leetcode\\.com$';
http://www.lryc.cn/news/195566.html

相关文章:

  • 中断机制-通过volatile实现线程中断停止
  • Elasticsearch 8.11 中的合并更少,摄取更快
  • 算法村开篇
  • Leetcode—136.只出现一次的数字【简单】
  • 关于RNNoise、webrtc_ns、三角带通滤波器、对数能量
  • c语言练习89:链表的使用
  • ArkTS及openHarmony
  • Idea怎么配置Maven才能优先从本地仓库获取依赖
  • 聊聊HttpClient的DnsResolver
  • 剑指智能驾驶,智己LS6胜算几何?
  • 网络工程师知识点5
  • 未来展望:大型语言模型与 SQL 数据库集成的前景与挑战
  • SpringCloud-Hystrix
  • Ansible脚本进阶---playbook
  • pytorch 模型部署之Libtorch
  • Unity——数据存储的几种方式
  • 『heqingchun-ubuntu系统下安装cuda与cudnn』
  • Unity AI Muse 基础教程
  • pgsl基于docker的安装
  • idea设置某个文件修改后所在父文件夹变蓝色
  • 代码随想录训练营二刷第五十八天 | 583. 两个字符串的删除操作 72. 编辑距离
  • 秋日有感之秋诉-于光
  • ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法
  • 【微信小程序】6天精准入门(第2天:小程序的视图层、逻辑层、事件系统及页面生命周期)
  • 速学Linux丨一文带你打开Linux学习之门
  • 符尧:别卷大模型训练了,来卷数据吧!【干货十足】
  • 2023年中国半导体检测仪器设备销售收入、产值及市场规模分析[图]
  • 诊断DLL——Visual Studio安装与dll使用
  • 专业课138,总分390+,西工大,西北工业大学827信号与系统考研分享
  • css3链接