牛客题霸-SQL进阶篇(刷题记录二)

本文基于前段时间学习总结的 MySQL 相关的查询语法,在牛客网找了相应的 MySQL 题目进行练习,以便加强对于 MySQL 查询语法的理解和应用。

由于涉及到的数据库表较多,因此本文不再展示,只提供 MySQL 代码与示例输出。

部分题目因为较难,附上题目解法讨论的链接供大家参考。

SQL 题目

SQL 136:查询每类试卷得分的前 3 名,如果两人最大分数相同,选择最小分数大者,如果还相同,选择 uid 大者

sql 复制代码
select tag as tid, uid, ranking
from(
	select tag, uid,
	rank() over(partition by tag order by max(score) desc, min(score) desc, uid desc) as ranking
	from examination_info ei
	join exam_record er
	on ei.exam_id = er.exam_id
	group by tag, uid
) b
where ranking <= 3

SQL 139:查询每个人近三个有试卷作答记录的月份中没有试卷是未完成状态的用户的试卷作答完成数,按试卷完成数和用户 ID 降序排列

sql 复制代码
select uid, count(score) as exam_complete_cnt
from(
	select *,
	dense_rank() over(partition by uid order by month(start_time) desc) as date_rank
	from exam_record
) b
where date_rank <= 3
group by uid
having count(score) = count(uid)
order by exam_complete_cnt desc, uid desc

SQL 143:查询每份试卷每月作答数和截止当月的作答总数

sql 复制代码
select exam_id, date_format(start_time, '%Y%m') as start_month,
count(start_time) as month_cnt,
sum(count(start_time)) over(partition by exam_id order by date_format(start_time, '%Y%m')) as cum_exam_cnt
from exam_record
group by exam_id, start_month

SQL 145:查询未完成状态的试卷的未完成数 incomplete_cnt 和未完成率 incomplete_rate

sql 复制代码
select exam_id, (count(exam_id)-count(score)) as incomplete_cnt,
round((count(exam_id)-count(score))/count(exam_id), 3) as complete_rate
from exam_record
group by exam_id
having incomplete_cnt >= 1

SQL 146:查询每个 0 级用户所有的高难度试卷考试平均用时和平均得分,未完成的默认试卷最大考试时长和 0 分处理

sql 复制代码
select ui.uid,
round(avg(if(score is null, 0, score)),0) as avg_score, 
round(avg(if(submit_time is null, duration, timestampdiff(minute, start_time, submit_time))), 1) as avg_time_took
from user_info ui
join exam_record er
on ui.uid = er.uid
join examination_info ei
on er.exam_id = ei.exam_id
where level = 0 and difficulty = 'hard'
group by uid

SQL 147:查询昵称以 '牛客' 开头 '号' 结尾、成就值在 1200~2500 之间,且最近一次活跃(答题或作答试卷)在 2021 年 9 月的用户信息

sql 复制代码
select ui.uid,nick_name,achievement
from user_info as ui
left join practice_record pr
on ui.uid = pr.uid
left join exam_record er
on ui.uid = er.uid
where nick_name like '牛客%号' 
and achievement between 1200 and 2500
group by ui.uid
having date_format(max(er.start_time),'%Y%m')=202109 
or date_format(max(pr.submit_time),'%Y%m')=202109

SQL 150:试卷得分按分界点 90, 75, 60 分为优良中差四个得分等级(分界点划分到左区间),查询不同用户等级的人在完成过的试卷中各得分等级占比(结果保留3位小数),未完成过试卷的用户无需输出,结果按用户等级降序、占比降序排序)(难点:窗口函数 + case when)

sql 复制代码
select *,
round(count(*)/sum(count(*)) over(partition by level), 3) as ratio
from (
	select level,
	case
	when score < 60 then '差'
	when score >= 60 and score < 75 then '中'
	when score >= 75 and score < 90 then '良'
	when score >= 90 then '优'
	end as score_grade
	from user_info ui
	join exam_record er
	on ui.uid = er.uid
) a
where score_grade is not null
group by level, score_grade
order by level desc, ratio desc

# 极简洁版本:仅窗口函数
select level,
case when score >= 90 then '优'
when score >= 75 then '良'
when score >= 60 then '中'
else '差' end as score_grade,
round(count(*)/sum(count(*)) over(partition by level), 3) as ratio
from exam_record
left join user_info using(uid)
where score is not null
group by level, score_grade
order by level desc, ratio desc


链接: SQL 150 题目解法讨论

SQL 151:查询注册时间最早的 3 个人

sql 复制代码
# 方法一
select uid, nick_name, register_time
from user_info
order by register_time
limit 3

# 方法二:窗口函数
select uid, nick_name, register_time
from(
	select uid, nick_name, register_time,
	rank() over(order by register_time) as ranking
	from user_info
) a
where ranking <= 3

SQL 152:查询求职方向为算法工程师,且注册当天就完成了算法类试卷的人,按参加过的所有考试最高得分排名。采用分页展示,每页 3 条,需要取出第 3 页(页码从 1 开始)的人的信息

sql 复制代码
select ui.uid, level, register_time, max(score) as max_score
from user_info ui
join exam_record er
on ui.uid = er.uid
join examination_info ei
on er.exam_id = ei.exam_id
where ui.job = '算法' and ei.tag = '算法'
and date_format(register_time, '%Y%m') = date_format(submit_time, '%Y%m')
group by ui.uid, level
order by max_score desc
limit 6, 3
相关推荐
一笑的小酒馆8 小时前
Android中的tcp通信
android
默阳123411 小时前
2026年Android中高级面试题专栏(Android进阶篇)
android
这个DBA有点耶11 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
古法安卓12 小时前
Android-日志系统源码解析
android·java·android studio
恋猫de小郭13 小时前
Android 17 + OkHttp 5.5.0 ,全新 ECH 下你的 HTTPS 域名可以请求时被安全隐藏
android·前端·flutter
小强闯江湖13 小时前
不只是让 AI 写代码:ViewCompose 把 Android UI 做成了可编译、可渲染的闭环
android·人工智能·kotlin
这个DBA有点耶14 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码14 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
hunterandroid14 小时前
StateFlow 与 SharedFlow 的边界:状态与事件的正确建模
android·前端
YF021114 小时前
Android遥控器对频详解
android·android things