-- 1. 创建学生表
CREATE TABLE t_student (
sid INT NOT NULL AUTO_INCREMENT COMMENT '学号',
sname VARCHAR(40) NOT NULL COMMENT '名称',
birthday DATE NOT NULL COMMENT '年龄',
ssex TINYINT NOT NULL DEFAULT 1 COMMENT '1男,2女',
PRIMARY KEY (sid)
);
-- 添加数据
INSERT INTO t_student VALUES(1, '赵雷' , '1990-01-01' , 1);
INSERT INTO t_student VALUES(2 , '钱电' , '1990-12-21' , 1);
INSERT INTO t_student VALUES(3 , '孙风' , '1990-12-20' , 1);
INSERT INTO t_student VALUES(4 , '李云' , '1990-12-06' , 1);
INSERT INTO t_student VALUES(5 , '周梅' , '1991-12-01' , 2);
INSERT INTO t_student VALUES(6 , '吴兰' , '1992-01-01' , 2);
INSERT INTO t_student VALUES(7 , '郑竹' , '1989-01-01' , 2);
INSERT INTO t_student VALUES(9 , '张三' , '2017-12-20' , 2);
INSERT INTO t_student VALUES(10 , '李四' , '2017-12-25' , 2);
INSERT INTO t_student VALUES(11 , '李四' , '2012-06-06' , 2);
INSERT INTO t_student VALUES(12 , '赵六' , '2013-06-13' , 2);
INSERT INTO t_student VALUES(13 , '孙七' , '2014-06-01' , 2);
-- 2.创建教师表
CREATE TABLE t_teacher (
tid INT NOT NULL AUTO_INCREMENT COMMENT '教师ID',
tname VARCHAR(40) NOT NULL COMMENT '教师名称',
PRIMARY KEY (tid)
);
-- 添加收据
INSERT INTO t_teacher VALUES(1 , '张五哥');
INSERT INTO t_teacher VALUES(2 , '李卫');
INSERT INTO t_teacher VALUES(3 , '年羹尧');
-- 3.课程表
CREATE TABLE t_course (
cid INT NOT NULL COMMENT '课程ID',
cname VARCHAR(50) COMMENT '课程名称',
tid INT COMMENT '教师id',
PRIMARY KEY (cid)
);
-- 添加数据
INSERT INTO t_course VALUES(1 , '语文' , 2);
INSERT INTO t_course VALUES(2 , '数学' , 1);
INSERT INTO t_course VALUES(3 , '英语' , 3);
-- 4.成绩表
CREATE TABLE t_score (
sid INT NOT NULL COMMENT '学号,外键',
cid INT NOT NULL COMMENT '课程id',
score decimal(5,2) COMMENT '成绩',
UNIQUE KEY ak_key_sid_cid (sid, cid)
);
-- 添加数据
INSERT INTO t_score VALUES(1 , 1 , 80);
INSERT INTO t_score VALUES(1 , 2 , 90);
INSERT INTO t_score VALUES(1 , 3 , 99);
INSERT INTO t_score VALUES(2 , 1 , 70);
INSERT INTO t_score VALUES(2 , 2 , 60);
INSERT INTO t_score VALUES(2 , 3 , 80);
INSERT INTO t_score VALUES(3 , 1 , 80);
INSERT INTO t_score VALUES(3 , 2 , 80);
INSERT INTO t_score VALUES(3 , 3 , 80);
INSERT INTO t_score VALUES(4 , 1 , 50);
INSERT INTO t_score VALUES(4 , 2 , 30);
INSERT INTO t_score VALUES(4 , 3 , 20);
INSERT INTO t_score VALUES(5 , 1 , 76);
INSERT INTO t_score VALUES(5 , 2 , 87);
INSERT INTO t_score VALUES(6 , 1 , 31);
INSERT INTO t_score VALUES(6 , 3 , 34);
INSERT INTO t_score VALUES(7 , 2 , 89);
INSERT INTO t_score VALUES(7 , 3 , 98);
01)查询" 1 "课程比" 2 "课程成绩高的学生的信息及课程分数
select * from t_student stu
INNER JOIN
(select c1.sid,c1.cid i1,c1.score s1,c2.cid i2 ,c2.score s2 from
(select * from t_score where cid =1)c1
INNER JOIN
(select * from t_score where cid =2) c2
on c1.sid=c2.sid) cc
on cc.sid = stu.sid
02)查询同时存在" 01 "课程和" 02 "课程的情况
select * from t_student stu
INNER JOIN
(select c1.sid ,c1.cid c1id,c2.cid c2cid from
(select * from t_score where cid =1) c1
INNER JOIN
(select * from t_score where cid =2) c2
on c1.sid=c2.sid) cc
on cc.sid=stu.sid
03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select
t.sid,
sum(case when t.cid=1 then t.score else 0 end),
sum(case when t.cid=2 then t.score else null end)
from t_score t GROUP BY t.sid
04)查询不存在" 01 "课程但存在" 02 "课程的情况
select t.sid,
sum(case when t.cid=1 then t.score else null end ),
sum(case when t.cid=2 then t.score else 0 end )
from t_score t GROUP BY t.sid
05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select aa.sid,st.*,aa.s1 from t_student st
INNER JOIN
(select * from (
select sid,avg(score) s1 from t_score GROUP BY sid
) t where t.s1 >= 60) aa
on aa.sid=st.sid
06)查询在t_score表存在成绩的学生信息
select * from t_student t where exists (select * from t_score ts where ts.sid=t.sid)
select DISTINCT stu.sid,stu.sname,stu.birthday from t_score t INNER JOIN (select * from t_student) stu on t.sid=stu.sid
07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select sid,asname,ccid,count(score) from
(
select * from
(select * from
(select sid asid,sname asname from t_student) a
INNER JOIN
(select sid bsid,count(cid) ccid from t_score GROUP BY sid) b
on a.asid=b.bsid
) c
INNER JOIN
(select * from t_score) d
on c.asid=d.sid
) e GROUP BY sid
08)查询「李」姓老师的数量
select count(tid) from t_teacher where tname like '李%';
09)查询学过「张三」老师授课的同学的信息
select * from t_student c
INNER JOIN
(select a.sid from
(select * from t_score) a
INNER JOIN
(select tid from t_teacher where tname like '李卫') b
on a.cid=b.tid) d
on d.sid=c.sid
10)查询没有学全所有课程的同学的信息
SELECT t1.`sid`,t1.`sname`,t1.`ssex`, t2.courses
FROM t_student t1
LEFT JOIN (SELECT sid,COUNT(*) courses FROM t_score GROUP BY sid) t2 ON t1.sid = t2.sid
WHERE t2.courses < (SELECT COUNT(*) FROM t_course) OR t2.courses IS NULL;
11)查询没学过"张三"老师讲授的任一门课程的学生姓名
select distinct stu.sname from t_student stu
INNER JOIN
(select * from t_score t
INNER JOIN
(select tid from t_teacher where tname !='李卫') tt
on t.cid=tt.tid
) ss
on stu.sid=ss.sid
12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT t3.sid, t3.sname, ROUND(AVG(t1.score), 2) scoreavg
FROM t_score t1
INNER JOIN (
-- 统计两门以上不及格的学生id
SELECT t.sid, COUNT(*) num FROM (SELECT sid, cid FROM t_score WHERE score < 60) t GROUP BY t.sid HAVING num >= 2
) t2 ON t1.sid = t2.sid
INNER JOIN t_student t3 ON t1.sid = t3.sid
GROUP BY t3.sid, t3.sname
13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT t1.`sid`,t1.`sname`,t3.score
FROM t_student t1
INNER JOIN (SELECT sid,score FROM t_score WHERE cid = 1 AND score < 60) t3
ON t1.`sid` = t3.sid
ORDER BY t3.score DESC;
14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT t2.sid,t2.sname ,
SUM(CASE WHEN t1.cid = 1 THEN t1.score ELSE 0 END) '语文',
SUM(CASE WHEN t1.cid = 2 THEN t1.score ELSE 0 END) '数学',
SUM(CASE WHEN t1.cid = 3 THEN t1.score ELSE 0 END) '英语',
ROUND(AVG(t1.`score`), 2) avgscore
FROM t_score t1
INNER JOIN t_student t2 ON t1.sid = t2.sid
GROUP BY t2.sid, t2.sname
ORDER BY avgscore DESC;
15)查询各科成绩最高分、最低分和平均分:
SELECT t2.cid '课程ID',
t2.cname '课程名称',
MAX(t1.score) '最高分',
MIN(t1.score) '最低分',
ROUND(AVG(t1.score), 2) '平均分',
COUNT(t1.sid) '选修人数',
ROUND(SUM(CASE WHEN t1.score >= 60 THEN 1 ELSE 0 END) / COUNT(t1.sid), 2) '及格率',
ROUND(SUM(CASE WHEN t1.score >=70 AND t1.score < 80 THEN 1 ELSE 0 END)/COUNT(t1.sid),2) '中等率',
ROUND(SUM(CASE WHEN t1.score >=80 AND t1.score < 90 THEN 1 ELSE 0 END)/COUNT(t1.sid),2) '优良率',
ROUND(SUM(CASE WHEN t1.score >= 90 THEN 1 ELSE 0 END)/COUNT(t1.sid), 2) '优秀率'
FROM t_score t1
INNER JOIN t_course t2 ON t1.cid = t2.cid
GROUP BY t2.cid, t2.cname
ORDER BY COUNT(t1.sid) DESC, t2.cid ASC;
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
本篇文章有一些Bug,请点击链接
地狱级查询