【牛客刷题--SQL篇】分组查询SQL18分组计算练习题&&SQL19分组过滤练习题&&SQL20分组排序练习题
个人主页:@与自己作战 大数据领域创作者
牛客刷题系列篇:【SQL篇】【Python篇】【Java篇】
推荐刷题网站注册地址:【牛客网–SQL篇】
推荐理由:从0-1起步,循序渐进
网址注册地址:【牛客网–注册地址】
文章目录
- 一、高级查询
- 1、分组查询
-
- 1.1 SQL18 分组计算练习题
-
- 1.1.1 SQL语句(第一种解法)
- 1.1.1 SQL语句(第二种解法推荐)
- 1.2 SQL19 分组过滤练习题
-
- 1.2.1 SQL语句(第一种解法)
- 1.2.2 SQL语句(第二种解法推荐)
- 1.3 SQL20 分组排序练习题
-
- 1.3.1 SQL语句(推荐写法)
一、高级查询
1、分组查询
1.1 SQL18 分组计算练习题
- 描述
题目:现在运营想要对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。
- 示例1
输入:
drop table if exists user_profile;
CREATE TABLEuser_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.0male|复旦大学|2|12.0|5.5female|北京大学|1|12.0|3.0female|浙江大学|1|5.0|1.0male|山东大学|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_cntfrom user_profilegroup by gender, university
1.2 SQL19 分组过滤练习题
- 描述
题目:现在运营想查看每个学校用户的平均发贴和回帖情况,寻找低活跃度学校进行重点运营,请取出平均发贴数低于5的学校或平均回帖数小于20的学校。
- 示例1
输入:
drop table if exists user_profile;
CREATE TABLEuser_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_cntfrom user_profilegroup by universityhaving 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,
round(avg(answer_cnt), 3) avg_answer_cnt
from
user_profile
group by
university
having
avg(question_cnt) < 5
or avg(answer_cnt) < 20
select university, round(avg(question_cnt), 3) avg_question_cnt, round(avg(answer_cnt), 3) avg_answer_cntfrom user_profilegroup by universityhaving avg(question_cnt) < 5 or avg(answer_cnt) < 20
1.3 SQL20 分组排序练习题
描述
题目:现在运营想要查看不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列,请你取出相应数据。
- 示例1
输入:
drop table if exists user_profile;
CREATE TABLEuser_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
int ,
answer_cnt
int
);
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);
输出:
浙江大学|1.0000
北京大学|2.5000
复旦大学|5.5000
山东大学|11.0000
输入: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` int ,`answer_cnt` int );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);复制输出:浙江大学|1.0000北京大学|2.5000复旦大学|5.5000山东大学|11.0000
1.3.1 SQL语句(推荐写法)
select
university,
round(avg(question_cnt), 4) avg_question_cnt
from
user_profile
group by
university
order by
round(avg(question_cnt), 4) asc
select university, round(avg(question_cnt), 4) avg_question_cntfrom user_profilegroup by universityorder by round(avg(question_cnt), 4) asc
推荐刷题网站:【牛客网–SQL篇】
网址注册地址:【牛客网–注册地址】