asp.net网站维护,岳阳建站公司,国外网站打不开怎么解决,企业网站的优劣势文章目录 【LeetCode高频SQL50题-基础版】打卡第9天#xff1a;第46~50题⛅前言患某种疾病的患者#x1f512;题目#x1f511;题解 第二高的薪水#x1f512;题目#x1f511;题解 按日期分组销售产品#x1f512;题目#x1f511;题解 列出指定时间段内所有的下单产品… 文章目录 【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.患某种疾病的患者 题解
考察知识点
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 cant 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.第二高的薪水 题解
考察知识点
分析直接可以使用窗口函数一下就成功解决了
方式一使用dense_rank
dese_rank按照指定分组进行排序不会跳过重复的序号112
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.按日期分组销售产品 题解
考察知识点group by、count、distinct、order by、group_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.列出指定时间段内所有的下单产品 题解
考察知识点group by、having、连接
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.查找拥有有效邮箱的用户 题解
考察知识点正则表达式
遇到这种字符串匹配很容易想到使用正则表达式
select user_id, name, mail
from Users
where mail regexp ^[a-zA-Z][a-zA-Z0-9_.-]*\\leetcode\\.com$;