牛客题霸-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中的外键约束

外键约束用于表示两张表中的指标连接关系。外键约束的作用主要有以下三点: 1.确保子表中的某个字段(外键)只能引用父表中的有效记录2.主表中的列被删除时,子表中的关联列也会被删除3.主表中的列更新时,子表中的关联元素也会被更新 子表中的元素指向主表 以下是一个外键约束的实例展示

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

如何去写一手好SQL

MySQL性能 最大数据量 抛开数据量和并发数,谈性能都是耍流氓。MySQL没有限制单表最大记录数,它取决于操作系统对文件大小的限制。 《阿里巴巴Java开发手册》提出单表行数超过500万行或者单表容量超过2GB,才推荐分库分表。性能由综合因素决定,抛开业务复杂度,影响程度依次是硬件配置、MySQL配置、数据表设计、索引优化。500万这个值仅供参考,并非铁律。 博主曾经操作过超过4亿行数据

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

MySQL数据库宕机,启动不起来,教你一招搞定!

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等)公众号:老苏畅谈运维欢迎关注本人公众号,更多精彩与您分享。 MySQL数据库宕机,数据页损坏问题,启动不起来,该如何排查和解决,本文将为你说明具体的排查过程。 查看MySQL error日志 查看 MySQL error日志,排查哪个表(表空间

MySQL高性能优化规范

前言:      笔者最近上班途中突然想丰富下自己的数据库优化技能。于是在查阅了多篇文章后,总结出了这篇! 数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意,并且最后不要超过32个字符 临时库表必须以tmp_为前缀并以日期为后缀,备份

[MySQL表的增删改查-进阶]

🌈个人主页:努力学编程’ ⛅个人推荐: c语言从初阶到进阶 JavaEE详解 数据结构 ⚡学好数据结构,刷题刻不容缓:点击一起刷题 🌙心灵鸡汤:总有人要赢,为什么不能是我呢 💻💻💻数据库约束 🔭🔭🔭约束类型 not null: 指示某列不能存储 NULL 值unique: 保证某列的每行必须有唯一的值default: 规定没有给列赋值时的默认值.primary key:

MySQL-CRUD入门1

文章目录 认识配置文件client节点mysql节点mysqld节点 数据的添加(Create)添加一行数据添加多行数据两种添加数据的效率对比 数据的查询(Retrieve)全列查询指定列查询查询中带有表达式关于字面量关于as重命名 临时表引入distinct去重order by 排序关于NULL 认识配置文件 在我们的MySQL服务安装好了之后, 会有一个配置文件, 也就

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

Java 连接Sql sever 2008

Java 连接Sql sever 2008 /Sql sever 2008 R2 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class TestJDBC