【牛客刷题--SQL篇】分组查询SQL18分组计算练习题&&SQL19分组过滤练习题&&SQL20分组排序练习题_sql分组查询练习题(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

一、高级查询

1、分组查询

1.1 SQL18 分组计算练习题

  • 描述

题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

在这里插入图片描述
在这里插入图片描述

  • 示例1

输入
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);

输出
male|北京大学|1|7.0|2.0
male|复旦大学|2|12.0|5.5
female|北京大学|1|12.0|3.0
female|浙江大学|1|5.0|1.0
male|山东大学|2|17.5|11.0

输入:
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);

输出:
male|北京大学|1|7.0|2.0
male|复旦大学|2|12.0|5.5
female|北京大学|1|12.0|3.0
female|浙江大学|1|5.0|1.0
male|山东大学|2|17.5|11.0

在这里插入图片描述

1.1.1 SQL语句(第一种解法)

select
gender,
university,
count(gender) user_num,
round(
sum(active_days_within_30) / count(active_days_within_30),
1
) avg_actie_day,
round(sum(question_cnt) / count(question_cnt), 1) avg_question_cnt
from
user_profile
group by
gender,
university

![在这里插入代码片](https://img-blog.csdnimg.cn/21d71970ec794ab5b7f94b4937e5d7cf.png)


在这里插入图片描述
在这里插入图片描述

1.1.1 SQL语句(第二种解法推荐)

select
gender,
university,
count(gender) as user_num,
round(avg(active_days_within_30), 1) as avg_active_day,
round(avg(question_cnt), 1) as avg_question_cnt
from
user_profile
group by
gender,
university

select
  gender,
  university,
  count(gender) as user_num,
  round(avg(active_days_within_30), 1) as avg_active_day,
  round(avg(question_cnt), 1) as avg_question_cnt
from
  user_profile
group by
  gender,
  university

在这里插入图片描述
在这里插入图片描述

1.2 SQL19 分组过滤练习题

  • 描述

题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
在这里插入图片描述

  • 示例1

输入
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);

输出
university|avg_question_cnt|avg_answer_cnt
北京大学|2.500|21.000
浙江大学|1.000|2.000

输入:
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);

输出:
university|avg_question_cnt|avg_answer_cnt
北京大学|2.500|21.000
浙江大学|1.000|2.000

在这里插入图片描述

1.2.1 SQL语句(第一种解法)

select
university,
round(sum(question_cnt) / count(question_cnt), 3) avg_question_cnt,
round(sum(answer_cnt) / count(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
round(sum(question_cnt) / count(question_cnt), 3) < 5
or round(sum(answer_cnt) / count(answer_cnt), 3) < 20

select
  university,
  round(sum(question_cnt) / count(question_cnt), 3) avg_question_cnt,
  round(sum(answer_cnt) / count(answer_cnt), 3) avg_answer_cnt
from
  user_profile
group by
  university
having
  round(sum(question_cnt) / count(question_cnt), 3) < 5
  or round(sum(answer_cnt) / count(answer_cnt), 3) < 20

在这里插入图片描述
在这里插入图片描述

1.2.2 SQL语句(第二种解法推荐)

select
university,
round(avg(question_cnt), 3) avg_question_cnt,

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

…(img-TFSHSqHp-1715369896666)]
[外链图片转存中…(img-afLKjDRZ-1715369896666)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值