猿创征文|第4天SQL快速入门-多表查询(SQL 小虚竹)
Posted 小虚竹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猿创征文|第4天SQL快速入门-多表查询(SQL 小虚竹)相关的知识,希望对你有一定的参考价值。
回城传送–》《32天SQL筑基》
文章目录
零、前言
今天是学习 SQL 打卡的第 4 天,每天我会提供一篇文章供群成员阅读( 不需要订阅付钱 )。
希望大家先自己思考,如果实在没有想法,再看下面的解题思路,自己再实现一遍。在小虚竹JAVA社区 中对应的 【打卡贴】打卡,今天的任务就算完成了,养成每天学习打卡的好习惯。
虚竹哥会组织大家一起学习同一篇文章,所以有什么问题都可以在群里问,群里的小伙伴可以迅速地帮到你,一个人可以走得很快,一群人可以走得很远,有一起学习交流的战友,是多么幸运的事情。
我的学习策略很简单,题海策略+ 费曼学习法。如果能把这些题都认认真真自己实现一遍,那意味着 SQL 已经筑基成功了。后面的进阶学习,可以继续跟着我,一起走向架构师之路。
今天的学习内容是:多表查询
一、练习题目
题目链接 | 难度 |
---|---|
子查询:SQL21 浙江大学用户题目回答情况 | ★★★☆☆ |
链接查询:SQL22 统计每个学校的答过题的用户的平均答题数 | ★★★☆☆ |
链接查询:SQL23 统计每个学校各难度的用户平均刷题数 | ★★★☆☆ |
链接查询:SQL24 统计每个用户的平均刷题数 | ★★★☆☆ |
组合查询:SQL25 查找山东大学或者性别为男生的信息 | ★★★☆☆ |
二、SQL思路
子查询:SQL21 浙江大学用户题目回答情况(inner join)
初始化数据
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
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
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
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);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,118,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,114,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
解法
要求统计:
- 查看所有来自浙江大学的用户
- 指定用户题目回答明细情况
分析:
- user_profile表取university 字段值是浙江大学的数据
SELECT
up.device_id
from user_profile up
where
up.university = ‘浙江大学’
- 用户表和用户题目回答明细表要关联,可用关键词:inner join
inner join(等值连接) 只返回两个表中联结字段相等的行
本题就是user_profile表中的device_id 字段跟question_practice_detail 表中的device_id进行关联
SELECT
up.device_id,
qpd.question_id,
qpd.result
from
user_profile up
inner join question_practice_detail qpd
where
up.university = '浙江大学'
and qpd.device_id = up.device_id
order by
qpd.question_id asc
链接查询:SQL22 统计每个学校的答过题的用户的平均答题数
初始化数据
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
CREATE TABLE `user_profile` (
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int
);
CREATE TABLE `question_practice_detail` (
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(2138,'male',21,'北京大学',3.4,7);
INSERT INTO user_profile VALUES(3214,'male',null,'复旦大学',4.0,15);
INSERT INTO user_profile VALUES(6543,'female',20,'北京大学',3.2,12);
INSERT INTO user_profile VALUES(2315,'female',23,'浙江大学',3.6,5);
INSERT INTO user_profile VALUES(5432,'male',25,'山东大学',3.8,20);
INSERT INTO user_profile VALUES(2131,'male',28,'山东大学',3.3,15);
INSERT INTO user_profile VALUES(4321,'male',28,'复旦大学',3.6,9);
INSERT INTO question_practice_detail VALUES(2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(6543,111,'right');
INSERT INTO question_practice_detail VALUES(2315,115,'right');
INSERT INTO question_practice_detail VALUES(2315,116,'right');
INSERT INTO question_practice_detail VALUES(2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(5432,118,'wrong');
INSERT INTO question_practice_detail VALUES(5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(2131,114,'right');
INSERT INTO question_practice_detail VALUES(5432,113,'wrong');
解法
要求统计:
- 查找每个学校用户的平均答题数目
- 某学校用户平均答题数量计算方式为该学校用户答题总次数除以答过题的不同用户个数
- 结果保留4位小数
- 结果按照university升序排序
分析:
- 查找每个学校用户,这里就是要对user_profile 表中的university字段进行分组排序,使用关键词: group by
select
u.university
from
user_profile u
group by
u.university
- 求某学校用户平均答题数量:计算方式为该学校用户答题总次数除以答过题的不同用户个数
这里要对user_profile 表和question_practice_detail 进行链接,关键词:inner join on
链接字段为:device_id
select
u.university,
count(q.question_id) / count(DISTINCT u.device_id) as avg_answer_cnt
from
user_profile u inner join question_practice_detail q on u.device_id = q.device_id
group by
u.university
- 结果保留4位小数:要对avg_answer_cnt值进行四舍五入,使用关键词:round(字段/表达式,4)
select
u.university,
round(count(q.question_id) / count(DISTINCT u.device_id),4) as avg_answer_cnt
from
user_profile u inner join question_practice_detail q on u.device_id = q.device_id
group by
u.university
- 结果按照university升序排序,使用关键词:order by
select
u.university,
round(count(q.question_id) / count(DISTINCT u.device_id),4) as avg_answer_cnt
from
user_profile u
inner join question_practice_detail q on u.device_id = q.device_id
group by
u.university
order by
u.university
链接查询:SQL23 统计每个学校各难度的用户平均刷题数
初始化数据
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
drop table if exists `question_detail`;
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
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);
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);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(16,5432,112,以上是关于猿创征文|第4天SQL快速入门-多表查询(SQL 小虚竹)的主要内容,如果未能解决你的问题,请参考以下文章
猿创征文|第5天SQL快速入门-必会的常用函数(SQL 小虚竹)