参考资料:
https://blog.csdn.net/fashion2014/article/details/78826299
https://blog.csdn.net/paul0127/article/details/82529216
https://www.cnblogs.com/coder-wf/p/11128033.html
https://www.cnblogs.com/clwsec/p/11615615.html
https://www.cnblogs.com/qixuejia/p/3637735.html
https://zhuanlan.zhihu.com/p/43289968
https://blog.csdn.net/flycat296/article/details/63681089
表名和字段
–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数
测试数据
#学生表
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`)
);
#成绩表
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' , '女');
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);
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 stu.*,sco_1.s_score as s_score_01,sco_2.s_score as s_score_02 FROM Student as stu
LEFT JOIN Score as sco_1 ON stu.s_id = sco_1.s_id AND sco_1.c_id = '01'
LEFT JOIN Score as sco_2 ON stu.s_id = sco_2.s_id AND sco_2.c_id = '02'
where sco_1.s_score>sco_2.s_score;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT stu.*,sco_1.s_score as s_score_01,sco_2.s_score as s_score_02 FROM Student as stu
LEFT JOIN Score as sco_1 ON stu.s_id = sco_1.s_id AND sco_1.c_id = '01'
LEFT JOIN Score as sco_2 ON stu.s_id = sco_2.s_id AND sco_2.c_id = '02'
where sco_1.s_score<sco_2.s_score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT stu.s_id,stu.s_name,ROUND(AVG(sco.s_score),2) as avg_score FROM Student as stu
LEFT JOIN Score as sco ON stu.s_id = sco.s_id WHERE sco.s_score IS NOT NULL
GROUP BY stu.s_id HAVING avg_score <60;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
SELECT stu_1.s_id,stu_1.s_name,ROUND(AVG(sco.s_score),2) AS avg_score FROM Student stu_1
LEFT JOIN Score sco on stu_1.s_id = sco.s_id
GROUP BY stu_1.s_id HAVING avg_score <60
UNION
SELECT s_id,s_name,0 AS avg_score FROM Student WHERE s_id NOT IN (SELECT DISTINCT s_id FROM Score);
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT stu.s_id,stu.s_name,count(sco.c_id) AS sum_course,sum(sco.s_score) AS sum_score FROM Student stu
LEFT JOIN Score sco on stu.s_id=sco.s_id GROUP BY stu.s_id;
6、查询"李"姓老师的数量
SELECT count(t_id) FROM Teacher WHERE t_name LIKE '李%';
7、查询学过"张三"老师授课的同学的信息
SELECT stu.* FROM Student as stu JOIN Score AS sco ON stu.s_id = sco.s_id
WHERE sco.c_id IN (SELECT c_id FROM Course WHERE t_id =
(SELECT t_id FROM Teacher WHERE t_name = '张三'));
8、查询没学过"张三"老师授课的同学的信息
SELECT * FROM Student WHERE s_id NOT IN (
SELECT stu.s_id FROM Student stu join Score sco on stu.s_id=sco.s_id where sco.c_id IN(
SELECT cou.c_id FROM Course cou JOIN Teacher tea ON cou.t_id = tea.t_id WHERE tea.t_name ='张三'));
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT stu.* FROM Student stu,Score sco_1,Score sco_2
WHERE stu.s_id = sco_1.s_id AND stu.s_id = sco_2.s_id AND sco_1.c_id = '01' AND sco_2.c_id = '02';
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT stu.* FROM Student stu WHERE stu.s_id IN (SELECT s_id FROM Score WHERE c_id='01' )
AND stu.s_id NOT IN(SELECT s_id from Score where c_id='02');
11、查询没有学全所有课程的同学的信息
SELECT stu.* FROM Student stu LEFT JOIN Score sco ON stu.s_id=sco.s_id
GROUP BY sco.s_id HAVING count(sco.c_id)<(SELECT count(*) FROM Course);
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT stu.* FROM Student stu WHERE stu.s_id IN(
SELECT DISTINCT sco_1.s_id FROM Score sco_1 where sco_1.c_id IN(
SELECT sco_2.c_id from Score sco_2 where sco_2.s_id='01'));
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT Student.* FROM Student
#下面的语句是找到'01'同学学习的课程数
WHERE s_id IN (SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(s_id) = (SELECT COUNT(c_id) FROM Score WHERE s_id = '01'))
#下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们
AND s_id NOT IN (SELECT s_id FROM Score
WHERE c_id IN(
#下面的语句是找到‘01’同学没学过的课程
SELECT DISTINCT c_id FROM Score WHERE c_id NOT IN (SELECT c_id FROM Score WHERE s_id = '01')) GROUP BY s_id);
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT a.s_name FROM Student a WHERE a.s_id NOT IN (
SELECT s_id FROM Score WHERE c_id = (SELECT c_id FROM Course WHERE t_id =(SELECT t_id FROM Teacher WHERE t_name = '张三')));
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT stu.s_id,stu.s_name,ROUND(AVG(sco.s_score),2) FROM Student stu LEFT JOIN Score sco ON stu.s_id = sco.s_id
WHERE sco.s_score IS NOT NULL AND sco.s_id IN(
SELECT s_id FROM Score WHERE s_score<60 GROUP BY s_id having count(*)>=2)GROUP BY sco.s_id
16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT stu.*,sco.c_id,sco.s_score FROM Student stu,Score sco
WHERE stu.s_id = sco.s_id and sco.c_id='01' and sco.s_score<60 ORDER BY sco.s_score DESC;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT a.s_id,(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='01') AS 语文,
(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='02') AS 数学,
(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='03') AS 英语,
round(avg(s_score),2) as 平均分 FROM Score a GROUP BY a.s_id ORDER BY 平均分 DESC;
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
FROM Score a LEFT JOIN Course b ON a.c_id = b.c_id GROUP BY a.c_id,b.c_name
19、按各科成绩进行排序,并显示排名
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank
FROM Score t1 where t1.c_id='01'
order by t1.s_score desc) t1)
union
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank
FROM Score t1 where t1.c_id='02'
order by t1.s_score desc) t2)
union
(select * from (select
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank
FROM Score t1 where t1.c_id='03'
order by t1.s_score desc) t3)
20、查询学生的总成绩并进行排名
select a.s_id,
@i:=@i+1 as i,
@k:=(case when @score=a.sum_score then @k else @i end) as rank,
@score:=a.sum_score as score
from (select s_id,SUM(s_score) as sum_score from Score GROUP BY s_id ORDER BY sum_score DESC)a,
(select @k:=0,@i:=0,@score:=0)s
21、查询不同老师所教不同课程平均分从高到低显示
SELECT a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from Course a
left join Score b on a.c_id=b.c_id
left join Teacher c on a.t_id=c.t_id
GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select d.*,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from Score a,(select @i:=0)s where a.c_id='01'
ORDER BY a.s_score DESC
)c
left join Student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3
UNION
select d.*,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from Score a,(select @j:=0)s where a.c_id='02'
ORDER BY a.s_score DESC
)c
left join Student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3
UNION
select d.*,c.排名,c.s_score,c.c_id from (
select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from Score a,(select @k:=0)s where a.c_id='03'
ORDER BY a.s_score DESC
)c
left join Student d on c.s_id=d.s_id
where 排名 BETWEEN 2 AND 3;
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from Score a
left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`,
ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比
from Score GROUP BY c_id)b on a.c_id=b.c_id
left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`,
ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比
from Score GROUP BY c_id)c on a.c_id=c.c_id
left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`,
ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比
from Score GROUP BY c_id)d on a.c_id=d.c_id
left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`,
ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比
from Score GROUP BY c_id)e on a.c_id=e.c_id
left join Course f on a.c_id = f.c_id
24、查询学生平均成绩及其名次
select a.s_id,
@i:=@i+1 as '不保留空缺排名',
@k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名',
@avg_score:=avg_s as '平均分'
from (select s_id,ROUND(AVG(s_score),2) as avg_s from Score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b;
25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的
网友评论