相关精彩专题链接: 数据成就更好的你
一、数据源
1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
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('08' , '王菊' , '1990-01-20' , '女');
2.课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数
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);
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"课程成绩低的学生的信息及课程分数
select a.* ,b.s_score as 01_score,c.s_score as 02_score
from Student a
join Score b on a.s_id=b.s_id and b.c_id='01'
left join Score c on a.s_id=c.s_id and c.c_id='02'
where b.s_score>c.s_score
或者
select c.s_id,s_name,s_sex, s_birth,a.score01,b.score02 from Student c
join (select s_id,s_score score01 from Score where c_id="01")a on c.s_id=a.s_id
left join (select s_id,s_score score02 from Score where c_id="02")b on c.s_id=b.s_id
where a.score01>b.score02;
2.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩,平均成绩降序
select student.s_id, student.s_name,temp.avgscore from student
left join (select score.s_id,round(avg(s_score),2) avgscore from score group by s_id)temp
on student.s_id=temp.s_id
where temp.avgscore>=60
Order by temp.avgscore desc
3.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
select student.s_id, student.s_name,temp.avgscore from student
left join (select score.s_id,round(avg(s_score),2) avgscore from score group by s_id)temp
on student.s_id=temp.s_id
where (temp.avgscore<=60) or (temp.avgscore is null);
4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as course_num,sum(b.s_score) as all_score from
student a
left join score b on a.s_id=b.s_id
GROUP BY a.s_id,a.s_name;
5.询课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select a.c_id,b.c_name,MAX(s_score) as 最高分,MIN(s_score) as 最低分,ROUND(AVG(s_score),2) as 平均分,
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/count(*)),2) as 及格率,
ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/count(*)),2) as 中等率,
ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/count(*)),2) as 优良率,
ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/count(*)),2) as 优秀率
from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name
6.查询学生的总成绩并进行排名
select c.*,row_number() over(order by c.allscore desc) as "排名" from
(select a.s_id,a.s_name,sum(b.s_score) allscore from student a
left join score b on a.s_id=b.s_id
group by a.s_id,a.s_name)c;
7.查询各科成绩的前三名
select c.c_name,b.s_name,a.s_score,a.rank1 from
(select score.*,row_number() over(partition by c_id order by s_score desc) as rank1 from score)a
left join student b on a.s_id=b.s_id
left join course c on a.c_id=c.c_id
where a.rank1<4
8.查询学生报课程,课程数量,每个学生所报的课程显示在同一行
select
a.s_id,a.s_name,
group_concat(c.c_name) as "所报课程",
count(distinct(c.c_name)) as "课程数量" from Student a
left join Score b on a.s_id=b.s_id
left join Course c on b.c_id=c.c_id
group by a.s_id,a.s_name;
9.查询各学生的年龄
select s_id,s_name,s_birth,s_sex,
(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') -
(case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age
from student;
10.查询学过"张三"老师授课的同学的信息
select a.* from student a
left join score b on a.s_id=b.s_id
left join course c on b.c_id=c.c_id
left join teacher d on c.t_id=d.t_id
where d.t_name="张三"
11.查询没学过"张三"老师讲授的任一门课程的学生姓名
select m.* from student m left join
(select c.s_id from teacher a
join course b on a.t_id=b.t_id and a.t_name="张三"
join score c on b.c_id=c.c_id)n
on m.s_id=n.s_id
where n.s_id is null;
13.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select c.* from
(
select a.s_id,a.s_name,c.c_name,b.s_score,d.t_name,rank() over(order by b.s_score desc) rf
from student a
left join score b on a.s_id=b.s_id
left join course c on b.c_id=c.c_id
left join teacher d on c.t_id=d.t_id
where d.t_name="张三"
)c where c.rf=1;
14查询1990年出生的学生名单
select s_name ,s_birth from student where s_birth like '1990%';