牛客题霸-SQL入门篇(刷题记录二)

2024-03-18 23:52

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

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

以下内容是牛客题霸-SQL入门篇剩余的第 21-39 道题目的 SQL 代码答案。

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

部分题目因为较难,难以独立做出,故附上题目解法讨论的链接供大家参考。

SQL 题目

SQL 21:查询所有来自浙江大学的用户题目回答明细情况

select qpd.device_id, question_id, result
from question_practice_detail qpd
join user_profile up
on qpd.device_id = up.device_id
where up.university = '浙江大学'

在这里插入图片描述

SQL 22:查询每个学校用户的平均答题数目

select university, count(question_id)/count(distinct(up.device_id)) as avg_answer_cnt
from question_practice_detail qpd
join user_profile up
on qpd.device_id = up.device_id
group by university

在这里插入图片描述

SQL 23:查询不同学校、不同难度的用户平均答题量

select university, difficult_level, count(qpd.question_id)/count(distinct(qpd.device_id)) as avg_answer_cnt
from user_profile up
join question_practice_detail qpd
on up.device_id = qpd.device_id
join question_detail qd
on qpd.question_id = qd.question_id
group by university, difficult_level

在这里插入图片描述

SQL 24:查询山东大学中不同难度的用户平均答题量

from user_profile up
join question_practice_detail qpd
on up.device_id = qpd.device_id
join question_detail qd
on qpd.question_id = qd.question_id
group by university, difficult_level
having university = '山东大学'

在这里插入图片描述

SQL 25:查询学校为山东大学或者性别为男性的用户的 device_id、gender、age 和 gpa 数据(结果不去重)

select device_id, gender, age, gpa from user_profile
where university = "山东大学"
union all
select device_id, gender, age, gpa from user_profile 
where gender = "male"

在这里插入图片描述

SQL 26:查询 25 岁以下和以上的用户数量,age 为 null 也记为 25岁以下

select if(age < 25 or age is null, '25岁以下', '25岁及以上') as age_cut, 
count(*) as number
from user_profile
group by age_cut

在这里插入图片描述

SQL 27:查询 20 岁以下,20-24 岁,25 岁及以上三个年龄段用户的明细情况(若年龄为空请返回其他)

select device_id, gender, 
if(age < 20, '20岁以下', if(age between 20 and 24, '20-24岁', if(age >= 25, '25岁及以上', '其他'))) as age_cut
from user_profile

在这里插入图片描述

SQL 28:查询 2021 年 8 月每天用户练习题目的数量

select day(date) as day, count(question_id)
from question_practice_detail
where year(date) = 2021 and month(date) = 8
group by day

在这里插入图片描述

SQL 29:查询用户在某天刷题后第二天还会再来刷题的平均概率(平均次日留存率,难点)

select count(distinct q2.device_id, q2.date)/count(distinct q1.device_id, q1.date) as avg_ret
from question_practice_detail q1 
left join question_practice_detail q2
on q1.device_id=q2.device_id and datediff(q2.date,q1.date) = 1

在这里插入图片描述
链接:SQL 29 题目解法讨论


SQL 30:查询每个性别的用户分别有多少参赛者

select if(profile like '%female%', 'female', 'male') as gender, 
count(*) as number
from user_submit
group by gender

在这里插入图片描述

SQL 31:查询博客 URL 中的用户名

select device_id, substr(blog_url, 11) as user_name 
from user_submit

在这里插入图片描述

SQL 32:查询每个年龄的用户分别有多少参赛者

select substr(profile,12,2) as age, count(*)
FROM user_submit
group by age

在这里插入图片描述

SQL 33:查询每个学校 GPA 最低的同学

select device_id, u1.university, u1.gpa 
from user_profile u1
join(select university, min(gpa) as gpa from user_profilegroup by university
) u2
on u1.university = u2.university and u1.gpa = u2.gpa
order by u1.university

在这里插入图片描述

SQL 34:查询复旦大学的每个用户在 8 月份练习的总题目数和回答正确的题目数,对于在 8 月份没有练习过的用户,结果返回 0(难点)

select u.device_id, university, count(q.question_id) as question_cnt,
sum(if(result = "right", 1, 0)) as right_question_cnt
from user_profile u
left join question_practice_detail q
on u.device_id = q.device_id 
where university = '复旦大学' and (month(date) = 8 or date is null)
group by u.device_id

在这里插入图片描述
链接:SQL 34 题目解法讨论


SQL 35:查询浙江大学的用户在不同难度题目下答题的正确率情况,并按照准确率升序输出

select difficult_level, sum(if(result = "right", 1, 0))/count(qpd.result) as correct_rate
from user_profile u
join question_practice_detail qpd
on u.device_id = qpd.device_id
join question_detail qd
on qpd.question_id = qd.question_id
where university = '浙江大学'
group by difficult_level
order by correct_rate

在这里插入图片描述

SQL 36:查询用户信息表中的 device_id 和 age ,并按照 age 升序排序

select device_id, age from user_profile
order by age

在这里插入图片描述

SQL 37:查询用户信息表中的device_id,age 和 gpa,先按照 gpa 升序排序,再按照年龄升序排序

select device_id, gpa, age from user_profile
order by gpa, age

在这里插入图片描述

SQL 38:查询用户信息表中的 age 和 gpa,先按照 gpa 降序排序,再按照 age 降序排序

select device_id, gpa, age from user_profile
order by gpa desc, age desc

在这里插入图片描述

SQL 39:查询 2021 年 8 月份所有练习过题目的总用户数和练习过题目的总次数

select count(distinct device_id) as did_cnt,
count(question_id) as question_cnt
from question_practice_detail
where year(date) = 2021 and month(date) = 8

在这里插入图片描述

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



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

相关文章

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

Mac电脑如何通过 IntelliJ IDEA 远程连接 MySQL

《Mac电脑如何通过IntelliJIDEA远程连接MySQL》本文详解Mac通过IntelliJIDEA远程连接MySQL的步骤,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友跟... 目录MAC电脑通过 IntelliJ IDEA 远程连接 mysql 的详细教程一、前缀条件确认二、打开 ID

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum