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));
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','女');
CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT '',
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id));
insert into Course values(
'01','语文','02');
insert into Course values(
'02','数学','01');
insert into Course values(
'03','英语','03');
CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL default '',
PRIMARY KEY(t_id));
insert into Teacher values(
'01','张三');
insert into Teacher values(
'02','李四');
insert into Teacher values(
'03','王五');
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id,c_id));
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”的课程成绩高的所有学生的学号(重点)
2、查询平均成绩大于60分的学生的学号和平均成绩(简单,第二道重点)
3、所有成绩小于60分的学生信息
4、查询平均成绩小于60分的学生的学号和平均成绩,考虑没参加考试的情况
5、查询没学过“张三”老师课的学生的学号、姓名(重点)
--先查张三老师的学生id
select st.s_id from course c join teacher t on c.t_id=t.t_id
join score s on c.c_id=s.c_id join student st on st.s_id=s.s_id
where t.t_name='张三'
--然后用not in取出非张三的学生
select st.s_id,st.s_name from student st where st.s_id not in
(select st.s_id from course c join teacher t on c.t_id=t.t_id
join score s on c.c_id=s.c_id join student st on st.s_id=s.s_id
where t.t_name='张三');
6、查询学过“张三”老师所教的所有课的同学的学号、姓名(重点)
select st.s_id,st.s_name from course c join teacher t on
c.t_id=t.t_id join score s on c.c_id=s.c_id join student
st on st.s_id=s.s_id where t.t_name='张三';
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
select st.s_id,st.s_name from score s1 inner join score s2
on s1.s_id=s2.s_id join student st on s1.s_id=st.s_id where
s1.c_id=01 and s2.c_id=02;
8、查询学过编号为“01”的课程但没有学过编号为“02”的课程的学生的学号、姓名
select st.s_id,st.s_name from score join student st on
score.s_id=st.s_id where score.c_id=01 and st.s_id not
in (select s_id from score where c_id=02);
9、查询没有学全所有课的学生的学号、姓名(重点)
select s_id from student where s_id not in (select s_id from
score where s_id in
(select s_id from score where c_id=01) and s_id in
(select s_id from score where c_id=02) and s_id in
(select s_id from score where c_id=03))
10、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名(重点)
select distinct st.s_id,st.s_name from student st left join
score s on st.s_id=s.s_id where s.c_id in (select c_id from
score where s_id=01);
12、查询和“01”号同学所学课程完全相同的其他同学的学号(重点)
13、查询没学过"张三"老师讲授的任一门课程的学生姓名 和47题一样(重点,能做出来)