数据库
首页 > 数据库> > 数据分析 | MySQL45道练习题(updating)

数据分析 | MySQL45道练习题(updating)

作者:互联网

数据准备

创建学生表(Student)、教师表(Teacher)、科目表(Course)、成绩表(SC)

# 学生表 Student:
 
create table Student(
 
SId varchar(10) ,
 
Sname varchar(10),
 
Sage datetime,
 
Ssex varchar(10));
 
 
# 教师表 Teacher
 
create table Teacher(
 
TId varchar(10),
 
Tname varchar(10)); 
 
 
# 科目表 Course
 
create table Course(
 
CId varchar(10),
 
Cname nvarchar(10),
 
TId varchar(10)); 

 
# 成绩表 SC
 
create table SC(
 
SId varchar(10),
 
CId varchar(10),
 
score decimal(18,1)); 

插入数据

# 学生表 Student:
 
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' , '女');
 
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
 
insert into Student values('09' , '张三' , '2017-12-20' , '女');
 
insert into Student values('10' , '李四' , '2017-12-25' , '女');
 
insert into Student values('11' , '李四' , '2017-12-30' , '女');
 
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
 
insert into Student values('13' , '孙七' , '2018-01-01' , '女');
 
 
# 科目表 Course
 
insert into Course values('01' , '语文' , '02'); 
 
insert into Course values('02' , '数学' , '01'); 
 
insert into Course values('03' , '英语' , '03'); 
 
 
# 教师表 Teacher
 
insert into Teacher values('01' , '张三');
  
insert into Teacher values('02' , '李四'); 
 
insert into Teacher values('03' , '王五'); 
 
 
# 成绩表 SC
 
insert into SC values('01' , '01' , 80); 
 
insert into SC values('01' , '02' , 90); 
 
insert into SC values('01' , '03' , 99); 
 
insert into SC values('02' , '01' , 70); 
 
insert into SC values('02' , '02' , 60); 
 
insert into SC values('02' , '03' , 80); 
 
insert into SC values('03' , '01' , 80); 
 
insert into SC values('03' , '02' , 80); 
 
insert into SC values('03' , '03' , 80); 
 
insert into SC values('04' , '01' , 50); 
 
insert into SC values('04' , '02' , 30); 
 
insert into SC values('04' , '03' , 20); 
 
insert into SC values('05' , '01' , 76); 
 
insert into SC values('05' , '02' , 87); 
 
insert into SC values('06' , '01' , 31); 
 
insert into SC values('06' , '03' , 34); 
 
insert into SC values('07' , '02' , 89); 
 
insert into SC values('07' , '03' , 98); 

题目总览

1.1

1.2

1.3

2.0

3.0

1.1查询同时存在01课程和02课程的情况

【分析】左边是01的课程记录,右边是02课程的记录,sid能关联上的就是说明01,02课程记录都有,用子查询+inner join。

-- 子查询+inner join --
select *
from (select * from sc where cid = '01') a
inner join (select * from sc where cid = '02') b
on a.sid = b.sid;

-- 自关联 --
select *
from sc a
inner join sc b
on a.sid = b.sid
where a.cid = '01' and b.cid = '02';

1.2查询存在01课程但可能不存在02课程的情况(不存在时显示为null)

【分析】先找出存在01的课程记录,然后和自己的其他课程做关联,如果是02就关联上了,若不是02就关联不上, 用left join。

--- left join --
select *
from (select * from sc where cid = '01') a
left join (select * from sc where cid = '02') b
on a.sid = b.sid;

-- or --
select *
from (select * from sc where cid = '01') a
left join sc b
on a.sid = b.sid and b.cid = '02';

-- 自关联 --
select *
from sc a
left join sc b
on a.sid = b.sid and b.cid = '02'
where a.cid = '01';

1.3查询不存在01课程但存在02课程的学生情况

【分析】先找出不存在01课程的学生,这些学生里面学习过02课程的就是我们想要的结果

-- inner join --
select *
from (select * from sc where sid not in (select sid from sc where cid = '01')) a
inner join sc b
on a.sid = b.sid and cid = '02';

-- 常规 --
select *
from sc a
where sid not in (select sid from sc where cid = '01')
and cid = '02';

2.0查询平均成绩大于等于60分的学生编号和学生姓名和平均成绩

【分析】要求查询平均成绩大于等于60分的同学信息,首先确定实在成绩表里面找,找到这样的同学之后,用sid去学生信息表里面关联,就可以得到学生的姓名信息,关键就是找sid。

-- --
select a.sid,a.sname,b.avg
from student a
inner join
(select sid,avg(score) as avg_score
from sc 
group by sid
having avg(score) >= 60) b
on a.sid=b.sid; 

3.0查询再sc表存在成绩的学生信息

【分析】成绩表肯定都是有学生的,用成绩表左关联就可以得到学生信息。

select b.*
from sc a
left join student b
on a.sid = b.sid
group by b.sid;

标签:练习题,insert,01,into,02,values,sid,MySQL45,updating
来源: https://blog.csdn.net/weixin_42444823/article/details/122565671