因为数据的特殊性,请勿随意修改本人数据,内藏玄机。可随意增加,查询数据
-- 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 stu.sid,stu.sname,stu.ssex,c1.cid, c1.score, c2.cid, c2.score
FROM t_student stu
INNER JOIN (SELECT t1.sid, t1.cid, t1.score FROM t_score t1 WHERE t1.cid = 1 ) c1
ON stu.sid = c1.sid
INNER JOIN (SELECT t2.sid, t2.cid, t2.score FROM t_score t2 WHERE t2.cid = 2) c2
ON stu.sid = c2.sid
WHERE c1.score > c2.score
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 t1.sid,t1.cid,t1.score
FROM t_score t1
WHERE t1.cid = 2 AND t1.sid NOT IN (SELECT t2.sid FROM t_score t2 WHERE t2.cid = 1);
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表存在成绩的学生信息
1.select * from t_student t where exists (select * from t_score ts where ts.sid=t.sid)
2.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 stu.sid,stu.sname,stu.* ,count(cid) ccid,
sum(case when cid=1 then score else 0 end) c1,
sum(case when cid=1 then score else 0 end) c2,
sum(case when cid=1 then score else 0 end) c3
from t_student stu
left JOIN
t_score sco
on sco.sid=stu.sid GROUP BY sid
08)查询「李」姓老师的数量
select count(tid) from t_teacher where tname like '李%';
09)查询学过「张三」老师授课的同学的信息
SELECT t1.sid,t1.sname,t1.ssex, t4.tid, t4.tname
FROM t_student t1
INNER JOIN t_score t2 ON t1.sid=t2.sid
INNER JOIN t_course t3 ON t2.cid = t3.cid
INNER JOIN t_teacher t4 ON t3.tid = t4.tid
WHERE t4.tname = '李卫';
10)查询没有学全所有课程的同学的信息
select * from
(select stu.sid,stu.sname,stu.birthday,stu.ssex,aa.sid aasid from t_student stu
left JOIN
(select a.sid from (select sid,count(*) ccid from t_score GROUP BY sid) a where a.ccid=3)aa
on stu.sid=aa.sid ) cc where cc.aasid is null
11)查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT t1.sid, t1.sname,t4.tname
FROM t_student t1
LEFT JOIN t_score t2 ON t1.sid=t2.sid
LEFT JOIN t_course t3 ON t2.cid=t3.cid
LEFT JOIN t_teacher t4 ON t3.tid = t4.tid
WHERE t4.tname != '李卫' OR t4.tname IS NULL;
12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select aa.sid,aa.sname,avg(case when aa.score is null then 0 else aa.score end)
from (select stu.sid,stu.sname,sco.score from t_student stu
LEFT JOIN
t_score sco
on sco.sid=stu.sid
where score<60 or score is null) aa GROUP BY sid
14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select stu.sid,stu.sname,
sum(case when sco.cid=1 then sco.score else 0 end) '语文',
sum(case when sco.cid=2 then sco.score else 0 end) '数学',
sum(case when sco.cid=3 then sco.score else 0 end) '英语',
avg(case when sco.score is null then 0 else sco.score end) aa
from t_student stu
left JOIN
t_score sco on sco.sid=stu.sid GROUP BY stu.sid ORDER BY aa desc
15)查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
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;