创建表
创建学生表
1 | CREATE TABLE student |
创建课程表
1 | CREATE TABLE course |
创建学生课程分数表
1 | CREATE TABLE sc |
创建教师表
1 | CREATE TABLE teacher |
初始化表数据
初始化学生表数据
1 | insert into student |
初始化教师表数据
1 | insert into teacher |
初始化课程表数据
1 | insert into course |
初始化学生课程成绩表数据
1 | insert into sc |
SQL 查询问题
查询 “1” 课程比 “2” 课程成绩高的所有学生的学号
1 | select a.sid from (select sid,score from sc where cid = '1') a, (select sid,score from sc where cid = '2') b where a.score > b.score and a.sid = b.sid; |
查询平均成绩大于 60 分的同学的学号和平均成绩
1 | select sid, avg(score) from sc group by sid having avg(score) > 60; |
查询所有同学的学号、姓名、选课数、总成绩
1 | select s.sid,s.sname,count(sc.cid),sum(sc.score) from student s,sc sc where s.sid = sc.sid group by sid; |
查询姓 “李” 的老师的个数
1 | select count(tid) from teacher where tname like '李%'; |
查询没学过 “叶平” 老师课的同学的学号、姓名
1 | select s.sid, s.sname from student s where s.sid not in (select distinct(sc.sid) from sc, course c, teacher t where sc.cid = c.cid and t.tid = c.tid and t.tname = "叶平"); |
查询学过 “1” 并且也学过编号 “2” 课程的同学的学号、姓名
1 | select sid, sname from student where sid in (select sid from sc where cid = '2' and sid in (select sid from sc where cid = '1')); |
查询学过 “叶平” 老师所教的所有课的同学的学号、姓名
1 | select sid, sname from student |
查询所有课程成绩小于 60 分的同学的学号、姓名
1 | select distinct(s.sid), s.sname from sc, student s where sc.sid = s.sid and sc.score < 60; |
查询没有学全所有课的同学的学号、姓名
1 | select s.sid, s.sname from student s,sc where s.sid = sc.sid group by s.sid, s.sname having count(sc.cid) < (select count(cid) from course); |
查询至少有一门课与学号为 “1” 的同学所学相同的同学的学号和姓名
1 | select distinct(s.sid), s.sname from student s, sc where s.sid = sc.sid and sc.cid in (select cid from sc where sc.sid = '1'); |
查询至少学过学号为 “1” 同学所有一门课的其他同学学号和姓名
1 | select distinct s.sid, s.sname from student s, sc where s.sid = sc.sid and s.sid != '1' and sc.cid in (select cid from sc where sc.sid = '1'); |
删除学习 “叶平” 老师课的 SC 表记录
1 | delete from sc where cid in (select distinct(cid) from (select sc.cid from sc, course c, teacher t where sc.cid = c.cid and c.tid = t.tid and t.tname = '叶平') a); |
向 SC 表中插入一些记录,这些记录要求符合以下条件:1、没有上过编号 “2” 课程的同学学号;2、插入 “2” 号课程的平均成绩
1 | insert into sc (select sid, '2', (select avg(score) from sc where cid = '2') from student where sid not in (select sid from sc where cid = '2')); |
查询和 “2” 号的同学学习的课程完全相同的其他同学学号和姓名
1 | select sid, sname from student where sid in (select sid from sc where cid in (select cid from sc where sid = '2') group by sid having count(*) = (select count(*) from sc where sid = '2')); |
把 “SC” 表中 “叶平” 老师教的课的成绩都更改为此课程的平均成绩
1 | update sc set score = (select a from (select avg(sc_2.score) a from sc sc_2 where sc_2.cid = sc.cid) x) where sc.cid in (select course.cid from course, teacher where course.tid = teacher.tid and teacher.tname = '叶平'); |
按平均成绩从高到低显示所有学生的 “语文”、”数学”、”英语” 三门的课程成绩,按如下形式显示:学生ID,语文,数学,英语,有效课程数,有效平均分
1 | select sid as 学生ID,(select score from sc where sc.sid = t.sid and cid = '1') as 语文,(select score from sc where sc.sid = t.sid and cid = '2') as 数学,(select score from sc where sc.sid = t.sid and cid = '3') as 英语,count(*) as 有效课程数, avg(t.score) as 有效平均成绩 from sc as tgroup by sid order by avg(t.score); |
查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
1 | select cid as 课程ID, max(score) as 最高分, min(score) as 最低分 from sc group by cid; |