第二十四例:链接查询,统计每个用户的平均刷题数
- 题目地址:统计每个用户的平均刷题数牛客题霸牛客网 (nowcoder.com)
- 初始化数据
droptable if exists `user_profile`;
droptable if exists `question_practice_detail`;
CREATETABLE `user_profile` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`gender` varchar(14) NOTNULL,
`age` int ,
`university` varchar(32) NOTNULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATETABLE `question_practice_detail` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`question_id`int NOTNULL,
`result` varchar(32) NOTNULL
);
CREATETABLE `question_detail` (
`id` intNOTNULL,
`question_id`int NOTNULL,
`difficult_level` varchar(32) NOTNULL
);
INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERTINTO question_practice_detail VALUES(4,6543,111,'right');
INSERTINTO question_practice_detail VALUES(5,2315,115,'right');
INSERTINTO question_practice_detail VALUES(6,2315,116,'right');
INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(10,2131,113,'right');
INSERTINTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERTINTO question_practice_detail VALUES(12,2315,115,'right');
INSERTINTO question_practice_detail VALUES(13,2315,116,'right');
INSERTINTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(17,2131,113,'right');
INSERTINTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERTINTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(22,2131,113,'right');
INSERTINTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERTINTO question_detail VALUES(1,111,'hard');
INSERTINTO question_detail VALUES(2,112,'medium');
INSERTINTO question_detail VALUES(3,113,'easy');
INSERTINTO question_detail VALUES(4,115,'easy');
INSERTINTO question_detail VALUES(5,116,'medium');
INSERTINTO question_detail VALUES(6,117,'easy');
- 题目:请你写一个SQL查询,计算山东,不同难度的用户平均答题量,保留四位小数
- 分析1:先找出山东大学的用户:select university from user_profile where university='山东大学'
- 分析2:再找出不同难度的用户平均答题量:select university,difficult_level from user_profile u inner join question_practice_detail qpd on u.device_id=qpd.device_id inner join question_detail qd on qod.question_id = qd.question_id where university='山东大学' group by difficult_level;
- 分析3:求用户平均答题量:count(qpd.question_id)/count(distinct qpd.device_id)
- 示例代码
select
university,
difficult_level,
round(count(qpd.question_id)/count(distinct qpd.device_id),4) as avg_answer_cnt
from
user_profile u
inner join
question_practice_detail qpd
on
u.device_id=qpd.device_id
inner join
question_detail qd
on
qpd.question_id = qd.question_id
where
university='山东大学'
groupby
difficult_level;
第二十五例:组合查询,查找山东大学或者性别为男生的信息
droptable if exists `user_profile`;
droptable if exists `question_practice_detail`;
CREATETABLE `user_profile` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`gender` varchar(14) NOTNULL,
`age` int ,
`university` varchar(32) NOTNULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATETABLE `question_practice_detail` (
`id` intNOTNULL,
`device_id` intNOTNULL,
`question_id`int NOTNULL,
`result` varchar(32) NOTNULL
);
CREATETABLE `question_detail` (
`id` intNOTNULL,
`question_id`int NOTNULL,
`difficult_level` varchar(32) NOTNULL
);
INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERTINTO question_practice_detail VALUES(4,6543,111,'right');
INSERTINTO question_practice_detail VALUES(5,2315,115,'right');
INSERTINTO question_practice_detail VALUES(6,2315,116,'right');
INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(10,2131,113,'right');
INSERTINTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERTINTO question_practice_detail VALUES(12,2315,115,'right');
INSERTINTO question_practice_detail VALUES(13,2315,116,'right');
INSERTINTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(17,2131,113,'right');
INSERTINTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERTINTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERTINTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERTINTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERTINTO question_practice_detail VALUES(22,2131,113,'right');
INSERTINTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERTINTO question_detail VALUES(1,111,'hard');
INSERTINTO question_detail VALUES(2,112,'medium');
INSERTINTO question_detail VALUES(3,113,'easy');
INSERTINTO question_detail VALUES(4,115,'easy');
INSERTINTO question_detail VALUES(5,116,'medium');
INSERTINTO question_detail VALUES(6,117,'easy');
- 题目:分别查看学校为山东大学或者性别为男性的用户的device_id,gender,age,和gap数据,请取出相应结果,不去重
- 分析1:查看学校为山东大学的device_id,gender,age和gpa数据:select device_id,gender,age,gpa from user_file where university='山东大学';
- 分析2:性别为男性的用户的device_id,gender,age和gpa数据:select device_id ,gender,age,gpa from user_profile where gender='male';
- 需要使用union all把两个查询的数据都输出
- 示例代码
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';