牛客题霸-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中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名