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

2024-03-25 17:44

本文主要是介绍牛客题霸-SQL进阶篇(刷题记录二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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

SQL 题目

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

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 rankingfrom examination_info eijoin exam_record eron ei.exam_id = er.exam_idgroup by tag, uid
) b
where ranking <= 3

在这里插入图片描述

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

select uid, count(score) as exam_complete_cnt
from(select *,dense_rank() over(partition by uid order by month(start_time) desc) as date_rankfrom 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:查询每份试卷每月作答数和截止当月的作答总数

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

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 分处理

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 月的用户信息

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)

select *,
round(count(*)/sum(count(*)) over(partition by level), 3) as ratio
from (select level,casewhen score < 60 then '差'when score >= 60 and score < 75 then '中'when score >= 75 and score < 90 then '良'when score >= 90 then '优'end as score_gradefrom user_info uijoin exam_record eron 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 个人

# 方法一
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 rankingfrom user_info
) a
where ranking <= 3

在这里插入图片描述

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

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

在这里插入图片描述

这篇关于牛客题霸-SQL进阶篇(刷题记录二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/845760

相关文章

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也