牛客题霸-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_concat 用法)

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

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/281923.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

青海200MW光伏项目 35kV开关站图像监控及安全警示系统

一、背景 随着我国新能源产业的快速发展&#xff0c;光伏发电作为清洁能源的重要组成部分&#xff0c;得到了国家政策的大力扶持。青海作为我国光伏资源丰富地区&#xff0c;吸引了众多光伏项目的投资建设。在此背景下&#xff0c;为提高光伏发电项目的运行效率和安全性能&…

基于Java中的SSM框架实现万卷图书馆书籍借阅管理系统项目【项目源码+论文说明】

基于Java中的SSM框架实现万卷图书馆书籍借阅管理系统演示 摘要 图书管理系统&#xff0c;是一个由人、计算机等组成的能进行管理信息的收集、传递、加工、保存、维护和使用的系统。利用信息控制企业的行为&#xff1b;帮助企业实现其规划目标。 图书馆管理系统&#xff0c;能…

二、typescript基础语法

一、条件语句 二、函数 1、有名函数 function add(x:number, y:number):number {return x y;}2、匿名函数 let add function (x:number, y:number):number {return x y;}函数可选参数 function buildName(firstname: string, lastname?:string) {if (lastname) {return fi…

asp.net mvc 重新引导视图路径,改变视图路径

asp.net mvc 重新引导视图路径&#xff0c;改变视图路径 使用指定的控制器上下文和母版视图名称来查找指定的视图 通过本文学习&#xff0c;你可以根据该技法&#xff0c;去实现&#xff0c;站点自定义皮肤&#xff0c;手机站和电脑站&#xff0c;其他设备站点&#xff0c;在不…

3.面向对象中级

文章目录 包访问修饰符封装继承继承使用细节继承内存布局及细节 Supersuper使用细节super与this比较 overwrite多态对象的多态&#xff1a;向上转型&#xff1a;向下转型&#xff1a;多态细节动态绑定机制 Object类equalshashcodetoStringfinalize 包 区分相同名字的类&#x…

LeetCode讲解算法1-排序算法(Python版)

文章目录 一、引言问题提出 二、排序算法1.选择排序&#xff08;Selection Sort&#xff09;2.冒泡排序3.插入排序&#xff08;Insertion Sort&#xff09;4.希尔排序&#xff08;Shell Sort&#xff09;5.归并排序&#xff08;Merge Sort&#xff09;6.快速排序&#xff08;Qu…

linux之shell脚本基础

1.构建基础脚本 1.1 创建shell脚本 1.1.1 第一行需要指定使用的shell # 用作注释行.shell并不会处理脚本中的注释行,但是第一行的注释,会告诉shell使用哪个shell来运行脚本. #!/bin/bash 1.1.2 让shell找到你的脚本 直接运行脚本会提示-bash: a.sh: command not found.因…

Selenium 自动化 —— Selenium IDE录制、回放、导出Java源码

Hello Selenium 示例 之前我们在专栏的第一篇文章中演示了使用使用Selenium进行百度搜索的Hello world示例。 代码不复杂非常简单&#xff1a; public static void main(String[] args) {WebDriver driver null;try {// 设置Chrome驱动的路径 // System.setPro…

Javaweb学习记录(三)请求响应案例

下面为一个请求响应案例&#xff0c;postman发送请求&#xff0c;服务器响应将一个xml文件中的数据通过读取解析&#xff0c;将其用Result类标准的格式返回前端&#xff0c;在前端用json的方式显示 后端Controller代码 1、通过本类的字节码文件得到类加载器并寻找到需要解析的…

如何使用 ArcGIS Pro 生成TIN

三角网是一种常用于表示地表地形的数字地球模型&#xff08;DEM&#xff09;方式&#xff0c;我们可以通过 ArcGIS Pro 将等高线和高程点转换为TIN&#xff0c;这里为大家介绍一下转换方法&#xff0c;希望能对你有所帮助。 数据来源 教程所使用的数据是从水经微图中下载的高…

MATLAB环境下基于振动信号的轴承状态监测和故障诊断

故障预测与健康管理PHM分为故障预测和健康管理与维修两部分&#xff0c;PHM首先借助传感器采集关键零部件的运行状态数据&#xff0c;如振动信号、温度图像、电流电压信号、声音信号及油液分析等&#xff0c;提取设备的运行监测指标&#xff0c;进而实现对设备关键零部件运行状…

python 爬取杭州小区挂牌均价

下载chrome驱动 通过chrome浏览器的 设置-帮助-关于Google Chrome 查看你所使用的Chrome版本 驱动可以从这两个地方找: 【推荐】https://storage.googleapis.com/chrome-for-testing-publichttp://npm.taobao.org/mirrors/chromedriver import zipfile import os import r…

前端面试拼图-知识广度

摘要&#xff1a;最近&#xff0c;看了下慕课2周刷完n道面试题&#xff0c;记录并添加部分可参考的文档&#xff0c;如下... 1. 移动端H5 click有300ms延迟&#xff0c; 如何解决&#xff1f; 背景&#xff1a;double tap to zoom 移动端H5中的300ms点击延迟问题通常是由浏览…

【地图】腾讯地图 - InfoWindow 自定义信息窗口内容时,内容 html 嵌套混乱问题

目录 需求描述问题问题代码页面展示 解决原因解决办法解决代码页面展示 代码汇总注 需求描述 腾讯地图上画点位&#xff0c;点击点位展示弹框信息 问题 问题代码 // 打开弹框 openInfoWindow(position, content) {this.infoWindow new TMap.InfoWindow({map: this.map,posit…

当内外网的域名相同时,如何在外网解析同域名的网址

当内部网络和外部网络存在相同的域名&#xff0c;并且希望内部用户通过内部DNS服务器解析到外部网络上的该域名对应的公网IP地址时&#xff0c;需要在内部DNS服务器上采取一些特殊配置策略来实现这一目标。以下是一种通用的解决方案&#xff1a; 条件转发&#xff08;Condition…

初识GO语言

是由google公司推出的一门编程语言&#xff0c;12年推出的第一个版本 Go的特点 Go为什么能在最近的IT领域炙手可热 集python简洁&C语言的性能于一身 21世纪的C语言 顺应容器化时代的到来 区块链的崛起 学习一门编程语言可以划分为下面这三个步骤 安装 编译器 or 解…

使用华为云HECS服务器+nodejs开启web服务

简介: 在华为云HECS服务器上使用nodejs开启一个web服务。 目录 1.开通华为云服务器 2.远程登录 2.1 使用华为官方的网页工具登录 ​编辑 2.2 使用MobaXterm登录 3 安装node 3.1 下载 2. 配置环境变量 4. 安装express模块 5.开启外网访问 1.开通华为云服务器 这…

《大模型对齐方法》最新综述

源自&#xff1a;专知 “人工智能技术与咨询” 发布 大模型在人工智能领域取得了革命性的突破&#xff0c;但它们也可能带来潜在的担忧。为了解决这些担忧&#xff0c;引入了对齐技术&#xff0c;以使这些模型遵循人类的偏好和价值观。尽管过去一年取得了相当大的进展&#…

怎么做好独立站的SEO优化

随着全球贸易的蓬勃发展&#xff0c;越来越多的企业开始关注外贸市场&#xff0c;并将目光投向了外贸网站。然而&#xff0c;在竞争激烈的外贸市场中&#xff0c;如何写出吸引人的文章&#xff0c;以及如何优化网站以在搜索引擎中脱颖而出&#xff0c;成为了外贸独立网站必须面…

如何与手机共享笔记本电脑的互联网?这里提供详细步骤

这篇文章介绍了如何通过将手机变成Wi-Fi热点来与手机共享笔记本电脑的互联网连接。 如何共享笔记本电脑的互联网连接 你可以通过Wi-Fi或有线共享笔记本电脑的数据连接,具体取决于你的设置。 Windows Windows允许你通过ICS共享你的互联网连接。ICS,或称互联网连接共享,是W…