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

2024-03-19 10:20

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

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

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

部分题目因为较难,难以独立做出或代码仅在平台上运行不成功,故附上题目解法讨论的链接供大家参考。

SQL 题目

SQL 110:在表中插入相关数据(一)

insert into exam_record(uid, exam_id, start_time, submit_time, score) 
values
(1001, 9001, '2021-09-01 22:11:12', '2021-09-01 23:01:12', 90),
(1002, 9002, '2021-09-04 07:01:02', null, null)

SQL 111:在表中插入相关数据(二)

insert into exam_record_before_2021
select null, uid,exam_id, start_time, submit_time, score
from exam_record 
where year(submit_time) < 2021

SQL 112:在表中插入相关数据(三)

replace into examination_info (exam_id, tag, difficulty, duration, release_time) 
values (9003, 'SQL', 'hard', 90, '2021-01-01 00:00:00')

SQL 123:查询所有用户完成 SQL 类别高难度试卷得分的截断平均值(去掉一个最大值和一个最小值后的平均值)

select tag, difficulty, round((sum(score)-max(score)-min(score))/(count(score)-2), 1) as clip_avg_score
from exam_record er
join examination_info ei
on er.exam_id = ei.exam_id
where tag = 'SQL' and difficulty = 'hard'
group by tag, difficulty

在这里插入图片描述

SQL 124:查询总作答次数 total_pv、试卷已完成作答数 complete_pv 和已完成的试卷数 complete_exam_cnt

select count(exam_id) as total_pv,
count(score) as complete_pv,
count(distinct if(submit_time is not null, exam_id, null)) as omplete_exam_cnt
from exam_record

在这里插入图片描述


SQL 125:查询 SQL 试卷得分不小于该类试卷平均得分的用户最低得分

select score as min_score_over_avg
from exam_record er
join examination_info ei
on er.exam_id = ei.exam_id
where score >= (select avg(score)from exam_record erjoin examination_info eion er.exam_id = ei.exam_idwhere tag = 'SQL'
) and tag = 'SQL' 
order by min_score_over_avg
limit 1

在这里插入图片描述

SQL 126:查询 2021 年每个月里试卷作答区用户平均月活跃天数 avg_active_days 和月度活跃人数 mau

select date_format(submit_time, '%Y%m') as month,
round(count(distinct uid, date_format(submit_time,'%Y%m%d'))/count(distinct uid), 2) as avg_active_days,
count(distinct uid) as mau
from exam_record
where year(submit_time) = 2021
group by month

在这里插入图片描述

SQL 127:查询 2021 年每个月里用户的月总刷题数 month_q_cnt,日均刷题数 avg_day_q_cnt(按月份升序排序)以及该年的总体情况(难点:with rollup 用法简化代码)

select ifnull(date_format(submit_time, '%Y%m'), '2021汇总') as submit_month, 
count(question_id) as month_q_cnt,
round(count(question_id)/day(last_day(submit_time)),3) avg_day_q_cnt 
from practice_record
where year(submit_time) = 2021
group by date_format(submit_time, '%Y%m')
with rollup

在这里插入图片描述
链接 1:MySQL 中 with rollup 的用法

链接2:SQL 127 题目解法讨论

SQL 128:查询 2021 年每个未完成试卷作答数大于 1 的有效用户的用户 ID、未完成试卷作答数、完成试卷作答数、作答过的试卷 tag 集合,按未完成试卷数量由多到少排序(有效用户指完成试卷作答数至少为 1 且未完成数小于 5)(难点:group by 用法)

select uid, 
sum(case when submit_time is null then 1 else 0 end) as incomplete_cnt,
sum(case when submit_time is null then 0 else 1 end) as complete_cnt,
# 方法2
# sum(submit_time is null) as incomplete_cnt
# count(submit_time) as complete_cnt
group_concat(distinct date(start_time),':',tag separator ';') as detail
from exam_record er
join examination_info ei
on er.exam_id = ei.exam_id
where year(start_time) = 2021
group by uid
having complete_cnt >= 1 and incomplete_cnt > 1 and incomplete_cnt < 5
order by incomplete_cnt desc

在这里插入图片描述

链接 1:MySQL 中的 group_concat 函数

链接 2:SQL 128 题目解法讨论


SQL 129:查询当月均完成试卷数不小于 3 的用户们爱作答的类别及作答次数,按次数降序输出

select tag, count(tag) as tag_cnt
from exam_record er
join examination_info ei
on er.exam_id = ei.exam_id
where uid in(select uid from exam_recordgroup by uidhaving count(submit_time) / count(distinct date_format(submit_time, "%Y%m")) >= 3
)
group by tag
order by tag_cnt desc

在这里插入图片描述

SQL 130:查询每张 SQL 类别试卷发布后,当天 5 级以上的用户作答的人数 uv 和平均分 avg_score,并按人数降序,相同人数的按平均分升序

select ei.exam_id, count(distinct ui.uid) as uv, round(avg(score), 1) as avg_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 level > 5 and tag = 'SQL'
group by ei.exam_id
order by uv desc, avg_score asc

在这里插入图片描述

SQL 131:查询作答 SQL 类别的试卷得分 > 80 的人的用户等级分布,并按数量降序排序(保证数量都不同)

select level, count(level) as level_cnt
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 tag = 'SQL' and score > 80
group by level
order by level_cnt desc

在这里插入图片描述

SQL 132:查询作答 SQL 类别的试卷得分 > 80 的人的用户等级分布,并按数量降序排序(注意:自建数据库时,以下代码能正确运行)

select exam_id as tid, count(distinct uid) as uv, count(start_time) as pv
from exam_record
group by exam_id
union
select question_id as tid, count(distinct uid) as uv, count(submit_time) as pv
from practice_record
group by question_id
order by uv desc, pv desc

在这里插入图片描述

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



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

相关文章

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:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字