牛客题霸-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 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串