超经典MySQL练习50题,做完这些你的SQL就过关了
出品:Python数据之道
作者:Peter
编辑:Lemon
相信⼤多学习了 Mysql 数据库语⾔的同学都会上⽹练习来练⼿,⽽⼤部分的⼈肯定知道有⼀篇 Mysql 经典练习题50题的帖⼦,上⾯的题⽬基本上涵盖了 Mysql 查询语句的关键知识点。
笔者近期对⼜将这 50 题进⾏了练习,同时整理了相关的参考答案,有⾃⼰的思路和⽅法,也有参考⼤神们的。不得不说,这50题对SQL的提升真的很有帮助!
笔者使⽤的 MySQL 版本是MySQL 5.7.28。
在⽂末提供获鉴于 50 题篇幅太长,本⽂只展⽰了其中10题及笔者的思考,50 题完整版练习题以及笔者的答案实践已整理在pdf⽂件中,共有100多页,在⽂末提供获取的⽅法。
取的⽅法
建表和插⼊数据
在开始之前,先建⽴本⽂所需要的数据表格:
-- 建表
-- 学⽣表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
-- 课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
-- 教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);jquery考试题
-- 成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
-- 插⼊学⽣表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '⼥');
insert into Student values('06' , '吴兰' , '1992-03-01' , '⼥');
crontab的时间设置insert into Student values('07' , '郑⽵' , '1989-07-01' , '⼥');
insert into Student values('08' , '王菊' , '1990-01-20' , '⼥');
-- 课程表测试数据
insert into Course values('01' , '语⽂' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
mysql语句分类insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
题⽬1
题⽬要求
成绩⾼的学⽣的信息及课程分数
查询'01'课程⽐'02'课程成绩⾼
SQL实现
-- ⽅法1
select
a.*
,b.s_score as 1_score
,c.s_score as 2_score
from Student a
join Score b on a.s_id = b.s_id and b.c_id = '01' -- 两个表通过学号连接,指定01
left join Score c on a.s_id = c.s_id and c.c_id='02' or c.c_id is NULL -- 指定02,或者c中的c_id直接不存在-- 为NULL的条件可以不存在,因为左连接中会直接排除c表中不存在的数据,包含NULL
where b.s_score > c.s_score; -- 判断条件
-- ⽅法2:直接使⽤where语句
selectresample童装贵吗
a.*
,b.s_score as 1_score
,c.s_score as 2_score
float类型条件from Student a, Score b, Score c
where a.s_id=b.s_id -- 列出全部的条件
and a.s_id=c.s_id
and b.c_id='01'
and c.c_id='02'
and b.s_score > c.s_score; -- 前者成绩⾼
第⼆种⽅法实现:第⼆种⽅法实现
题⽬2
题⽬要求
查询'01'课程⽐'02'课程成绩低
成绩低的学⽣的信息及课程分数(题⽬1是成绩⾼)SQL实现
类⽐题⽬1的实现过程
-- ⽅法1:通过连接⽅式实现
select
a.*
,b.s_score as 1_score
,c.s_score as 2_score
from Student a
left join Score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL -- 包含NULL的数据
join score c on a.s_id=c.s_id and c.c_id='02'
where b.s_score < c.s_score;
-- 通过where⼦句实现
select
a.*
,b.s_score as 1_score
,c.s_score as 2_score
from Student a, Score b, Score c
where a.s_id=b.s_id
and a.s_id=c.s_id
and a.s_id=c.s_id
and b.c_id='01'
and c.c_id='02'
and b.s_score < c.s_score; -- 前者⽐较⼩
题⽬3
题⽬需求
查询平均成绩⼤于等于60分的同学的学⽣编号和学⽣姓名和平均成绩SQL实现
-- 执⾏顺序:先执⾏分组,再执⾏avg平均操作
select
b.s_id
,b.s_name
,
circulate的形容词round(avg(a.s_score), 2) as avg_score
from Student b
join Score a
on b.s_id = a.s_id
group by b.s_id -- 分组之后查询每个⼈的平均成绩
having avg_score >= 60;
-- 附加题:总分超过200分的同学
select
b.s_id
,b.s_name
,round(sum(a.s_score),2) as sum_score -- sum求和
from Student b
join Score a
on b.s_id=a.s_id
group by b.s_id
having sum_score > 200;
附加题:总分超过200分的同学
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论