一、表结构要求:
-- 1.学生表-t_student
-- sid 学生编号,sname 学生姓名,sage 学生年龄,ssex 学生性别
-- 2.教师表-t_teacher
-- tid 教师编号,tname 教师名称
-- 3.课程表-t_course
-- cid 课程编号,cname 课程名称,tid 教师名称
-- 4.成绩表-t_score
-- sid 学生编号,cid 课程编号,score 成绩
二、题目:
01)查询" 1 "课程比" 2 "课程成绩高的学生的信息及课程分数
02)查询同时存在" 01 "课程和" 02 "课程的情况
03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
04)查询不存在" 01 "课程但存在" 02 "课程的情况
05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
06)查询在t_score表存在成绩的学生信息
07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
08)查询「李」姓老师的数量
09)查询学过「张三」老师授课的同学的信息
10)查询没有学全所有课程的同学的信息
11)查询没学过"张三"老师讲授的任一门课程的学生姓名
12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
三、表数据:
-- 学生表
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);
-- 教师表
INSERT INTO t_teacher VALUES(1 , '张五哥');
INSERT INTO t_teacher VALUES(2 , '李卫');
INSERT INTO t_teacher VALUES(3 , '年羹尧');
-- 课程表
INSERT INTO t_course VALUES(1 , '语文' , 2);
INSERT INTO t_course VALUES(2 , '数学' , 1);
INSERT INTO t_course VALUES(3 , '英语' , 3);
-- 成绩表
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);
-- 测试库建库脚本
DROP TABLE IF EXISTS t_student;
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)
);
DROP TABLE IF EXISTS t_teacher;
CREATE TABLE t_teacher
(
tid INT NOT NULL AUTO_INCREMENT COMMENT '教师ID',
tname VARCHAR(40) NOT NULL COMMENT '教师名称',
PRIMARY KEY (tid)
);
DROP TABLE IF EXISTS t_course;
CREATE TABLE t_course
(
cid INT NOT NULL COMMENT '课程ID',
cname VARCHAR(50) COMMENT '课程名称',
tid INT COMMENT '教师id',
PRIMARY KEY (cid)
);
ALTER TABLE t_course ADD CONSTRAINT fk_ref_cou_tea_tid FOREIGN KEY (tid) REFERENCES t_teacher (tid) ON DELETE RESTRICT ON UPDATE RESTRICT;
DROP TABLE IF EXISTS t_score;
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)
);
ALTER TABLE t_score ADD CONSTRAINT fk_ref_sco_stu_sid FOREIGN KEY (sid) REFERENCES t_student (sid) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE t_score ADD CONSTRAINT fk_ref_sco_tea_cid FOREIGN KEY (cid) REFERENCES t_course (cid) ON DELETE RESTRICT ON UPDATE RESTRICT;
--学生表
select * from t_student;
--查询性别为男的
--成绩表
select * from t_score;
--课程表
select * from t_course;
select * from t_teacher;--老师表
-- 学生表
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);
-- 教师表
INSERT INTO t_teacher VALUES(1 , '张五哥');
INSERT INTO t_teacher VALUES(2 , '李卫');
INSERT INTO t_teacher VALUES(3 , '年羹尧');
-- 课程表
INSERT INTO t_course VALUES(1 , '语文' , 2);
INSERT INTO t_course VALUES(2 , '数学' , 1);
INSERT INTO t_course VALUES(3 , '英语' , 3);
-- 成绩表
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 where sid in(
select sid from
(select * from t_score where cid=1) a left join
(select * from t_score where cid=2) b
using(sid) where a.score>b.score
);
02)查询同时存在" 01 "课程和" 02 "课程的情况
select sid from
(select * from t_score where cid=1) a left join
(select * from t_score where cid=2) b
using(sid) where a.score>b.score
03)查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from
(select * from t_score where cid =1) s1
left join
(select * from t_score where cid =2) s2
using(sid)
04)查询不存在" 01 "课程但存在" 02 "课程的情况
select * from t_score where cid=2 and sid not in (
select sid from t_score where cid=1
)
05)查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select a.sid,b.sname, avg(score) from t_score a,t_student b
where a.sid=b.sid
group by a.sid
having avg(score)>60
06)查询在t_score表存在成绩的学生信息
select * from t_student where sid in(
select distinct sid from t_score
)
select * from t_student where sid not in(
select distinct sid from t_score
)
07)查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select sname,count(cid),ifnull(sum(score),0)
from t_student a left join t_score b
using(sid)
group by sid
08)查询「李」姓老师的数量
select * from t_teacher where tname like '李%'
09)查询学过「张三」老师授课的同学的信息
select d.sname,a.tname,b.cname from t_teacher a,
t_course b,
t_score c,
t_student d
where a.tid=b.tid and b.cid=c.cid and c.sid=d.sid
and a.tname='张五哥';
10)查询没有学全所有课程的同学的信息
select sname,count(cid)
from t_student a left join t_score b
using(sid)
group by sid
having count(cid)<(select count(1) from t_course);
-- 11)查询没学过"张五哥"老师讲授的任一门课程的学生姓名
select sname from t_student left join (
select stu.sid,score.cid from
t_teacher teacher,
t_course cou,t_score score,
t_student stu
where teacher.tid = cou.tid
and cou.cid=score.cid
and score.sid=stu.sid
and tname = '张五哥') a using(sid) where cid is null;
-- 12)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select stu.sid,stu.sname,AVG(score.score) from t_student stu,t_score score where stu.sid=score.sid and score.score<60 group by stu.sid having COUNT(stu.sid);
-- 13)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select * from t_student stu,t_score score
where stu.sid=score.sid
and score.cid=1
and score.score<60
order by score desc;
-- 14)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select stu.sid,stu.sname,score.score,avg.score from
t_student stu,
t_score score,
(select sid,avg(score) score from t_score GROUP BY sid) avg
where stu.sid=score.sid ORDER BY avg.score desc;