高频SQL50题(基础版)-3

文章目录

主要内容

  1. LeetCode-高频SQL50题(基础版)21-30

一.SQL练习题

1.1174-即时食物配送


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
Select round(avg(order_date = customer_pref_delivery_date)*100,2) immediate_percentage
from (
    select *,row_number() over(partition by customer_id order by order_date) as rn 
    from delivery 
) a 
where rn = 1

2.550-游戏玩法分析


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below

select round(avg(a.event_date is not null), 2) fraction
from 
    (select player_id, min(event_date) as login
    from activity
    group by player_id) p 
left join activity a 
on p.player_id=a.player_id and datediff(a.event_date, p.login)=1

3.2356-每位教师所教授的科目种类的数量


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select teacher_id,count(distinct subject_id) as cnt 
from teacher 
group by teacher_id;

4.1141-查询近30天活跃用户数


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
SELECT activity_date day, COUNT(DISTINCT  user_id) active_users
FROM Activity
WHERE DATEDIFF("2019-07-27", activity_date) < 30 AND DATEDIFF("2019-07-27", activity_date) >= 0
GROUP BY activity_date;

5.1084-销售分析


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select p.product_id,product_name 
from sales s,product p 
where s.product_id = p.product_id
group by p.product_id
having sum(sale_date < '2019-01-01') = 0
and sum(sale_date > '2019-03-31') = 0;

6.596-超过5名学生的课


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select class
from courses
group by class
having count(distinct(student))>=5;

7.1729-求关注者的数量


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select user_id, count(follower_id) as followers_count
from followers 
group by user_id
order by user_id;

8.619-只出现一次的最大数字



代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select max(num) as num
from (
    select num 
    from MyNumbers
    group by num 
    having count(num)=1) as t ;

9.1045-买下所有产品的客户


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select distinct c.customer_id
from customer c
left join product p 
on c.product_key = p.product_key
group by c.customer_id
having count(distinct p.product_key) = (select count(distinct product_key)from product)

10.1731-每位经理的下属员工数量


代码如下(示例):
sql 复制代码
# Write your MySQL query statement below
select 
    b.employee_id,
    b.name,
    count(a.reports_to) reports_count,
    round(avg(a.age),0) average_age
from employees a,employees b 
where a.reports_to = b.employee_id
group by a.reports_to
having reports_count != 0 
order by employee_id;

总结

以上是今天要讲的内容,练习了一些高频SQL题。

相关推荐
桀桀桀桀桀桀7 分钟前
数据库中的用户管理和权限管理
数据库·mysql
代码之光_198016 分钟前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi21 分钟前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
萨格拉斯救世主36 分钟前
戴尔R930服务器增加 Intel X710-DA2双万兆光口含模块
运维·服务器
Jtti39 分钟前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
yeyuningzi1 小时前
Debian 12环境里部署nginx步骤记录
linux·运维·服务器
superman超哥1 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452001 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库
颜淡慕潇1 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
EasyCVR2 小时前
萤石设备视频接入平台EasyCVR多品牌摄像机视频平台海康ehome平台(ISUP)接入EasyCVR不在线如何排查?
运维·服务器·网络·人工智能·ffmpeg·音视频