当前位置:网站首页>SQL Daily Practice (Nioke New Question Bank) - Day 5: Advanced Query
SQL Daily Practice (Nioke New Question Bank) - Day 5: Advanced Query
2022-08-03 08:37:00 【no envy】
1. 查找GPA最高值
题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据
建表语句:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float);
INSERT INTO user_profile VALUES(1,2234,'male',21,'北京大学',3.2);
INSERT INTO user_profile VALUES(2,2235,'male',null,'复旦大学',3.8);
INSERT INTO user_profile VALUES(3,2236,'female',20,'复旦大学',3.5);
INSERT INTO user_profile VALUES(4,2237,'female',23,'浙江大学',3.3);
INSERT INTO user_profile VALUES(5,2238,'male',25,'复旦大学',3.1);
INSERT INTO user_profile VALUES(6,2239,'male',25,'北京大学',3.6);
INSERT INTO user_profile VALUES(7,2240,'male',null,'清华大学',3.3);
INSERT INTO user_profile VALUES(8,2241,'female',null,'北京大学',3.7);
解题答案:
#Use aggregate function to take maximum value
select max(gpa)
from user_profile
where university = '复旦大学'
或者
#通过gpa倒叙,然后取第一条
select gpa
from user_profile
where university = '复旦大学'
order by gpa DESC
limit 1
2. 计算男生人数以及平均GPA
题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据.
建表语句:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8);
INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);
解题答案:
select
count(gender) male_num,
round(avg(gpa),1) avg_gpa
from user_profile GROUP BY gender having gender ='male'
3. 分组计算练习题
题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量.
建表语句:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` float,
`question_cnt` float,
`answer_cnt` float
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
解题答案:
SELECT gender,university,count(gender) as user_num,
avg(active_days_within_30) as avg_active_days,avg(question_cnt) as avg_question_cnt
from user_profile
GROUP by university,gender;
4. 分组过滤练习题
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校.
建表语句:
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` float,
`answer_cnt` float
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
解题答案:
SELECT
university,
avg( question_cnt ) AS avg_question_cnt,
avg( answer_cnt ) AS avg_answer_cnt
FROM
user_profile GROUP BY university
HAVING
avg_question_cnt < 5 OR avg_answer_cnt < 20
5. 如何让刷题变得更高效?
最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!
链接地址:牛客网 | SQL刷题篇,废话少说速度上号!!!
边栏推荐
- 文章列表的显示 以及创建文章 还有文章详情的基本
- English Grammar - Adverbial Clauses
- timestamp
- How does Mysql query two data tables for the same fields in two tables at the same time
- ArcEngine(四)MapControl_OnMouseDown的使用
- 《剑指Offer》刷题之打印从1到最大的n位数
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
- ArcEngine (2) loading the map document
- Alibaba Cloud SMS Sending
- 线性表
猜你喜欢
HCIP练习(OSPF)
[Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)
数仓4.0(二)------ 业务数据采集平台
使用pipreqs导出项目所需的requirements.txt(而非整个环境)
AI mid-stage sequence labeling task: three data set construction process records
dflow部署简记
线性表
MySQL2
dflow入门2——Slices
Nacos使用实践
随机推荐
Exception: Dataset not found. Solution
HCIP练习02(OSPF)
多线程下的单例模式
Unity编辑器扩展批量修改图片名称
Nacos使用实践
进程的创建
ArcEngine(五)用ICommand接口实现放大缩小
10 minutes to get you started chrome (Google) browser plug-in development
安装mysql-workbench
The window of the chosen data flow
frp:开源内网穿透工具
IDEA2021.2安装与配置(持续更新)
Charles packet capture tool learning record
WPF 学习笔记《WPF样式基础》
netstat 及 ifconfig 是如何工作的。
内存模型之有序性
SQL每日一练(牛客新题库)——第5天:高级查询
ArcEngine(一)加载矢量数据
Logic Pro X自带音色库列表
greenplum role /user 管理