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

文章目录

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

⛅前言

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

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

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

患某种疾病的患者

🔒题目

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

🔑题解

  • 考察知识点
mysql 复制代码
select min(id), email
from Person
group by email;

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

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

正确的SQL:

mysql 复制代码
delete from Person
where id not in (
  select min(id)
  from Person
  group by email
);

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

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

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

mysql 复制代码
delete p from Person p
left join (
  select min(id) id
  from Person
  group by email
) t 
on p.id = t.id
where t.id is null;

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

mysql 复制代码
delete p1 from Person p1, Person p2
where p1.email = p2.email and p1.id > p2.id;

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

第二高的薪水

🔒题目

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

🔑题解

  • 考察知识点

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

方式一:使用dense_rank

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

mysql 复制代码
select salary SecondHighestSalary
from(
    select salary, dense_rank() over(order by salary desc) ranking
    from Employee
) t
where ranking = 2;

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

mysql 复制代码
select if(max(ranking) < 2, null, salary) SecondHighestSalary
from(
    select salary, dense_rank() over(order by salary desc) ranking
    from Employee
) t
where ranking = 2;

方式二:使用nth_value

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

1)

mysql 复制代码
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)

mysql 复制代码
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或者更高的版本中使用,所以这里我们还算有必要学习如何使用普通函数来解决这一题

mysql 复制代码

按日期分组销售产品

🔒题目

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

🔑题解

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

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

1)

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

2)

mysql 复制代码
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.列出指定时间段内所有的下单产品

🔑题解

  • 考察知识点group byhaving连接
mysql 复制代码
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

这里还提供一种方法:

mysql 复制代码
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.查找拥有有效邮箱的用户

🔑题解

  • 考察知识点正则表达式

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

mysql 复制代码
select user_id, name, mail
from Users
where mail regexp '^[a-zA-Z][a-zA-Z0-9_.-]*\\@leetcode\\.com$';
相关推荐
逃逸线LOF5 分钟前
SpringMVC拦截器 & 异常处理机制
数据库·mysql
CoderYanger24 分钟前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
清晨0011 小时前
国产化数据库替代方案:openGauss 详解与 MySQL 平滑迁移实战
数据库·mysql
sheeta19983 小时前
LeetCode 每日一题笔记 日期:2026.09.02 题目:3875.构造奇偶一致的数组 I
笔记·算法·leetcode
夜雪一千4 小时前
MySQL如何限制账号权限,最小权限原则实践
数据库·mysql
退休倒计时4 小时前
【每日五题】leetcode TypeScript
算法·leetcode·职场和发展·typescript
2601_962056234 小时前
EasyMarkets易信:比特币基金流入创年内新高
人工智能·mysql
拾光Ծ5 小时前
【MySQL】复合查询、表的连接
java·数据库·sql·mysql
吃饱了得干活5 小时前
MySQL 进阶:锁与事务、执行计划、内存管理、高可用架构及 8.0 新特性
后端·mysql
心抵鹊5 小时前
力扣每日一题:计算右侧小于当前元素的个数(hard)
算法·leetcode