drop table tbl_教师表
--创建教师表
create table tbl_教师表(
id number not null primary key,
name varchar2(100)
)
--创建成绩表
drop table tbl_课程表
create table tbl_课程表(
id number not null primary key,
name varchar2(100),
teacherid number,
foreign key(teacherid) references tbl_教师表(id)
)
--创建学生表
drop table tbl_学生表
create table tbl_学生表(
id number not null primary key,
name varchar2(100)
)
--创建成绩表
drop table tbl_成绩表
create table tbl_成绩表(
studentid number,
courseid number,
score number,
foreign key(studentid) references tbl_学生表(id),
foreign key(courseid) references tbl_课程表(id)
)
--教师表插入数据
insert into tbl_教师表
values(1,'李老师')
insert into tbl_教师表
values(2,'王老师')
insert into tbl_教师表
values(3,'张老师')
--课程表插入数据
insert into tbl_课程表
values(001,'数学',1)
insert into tbl_课程表
values(002,'语文',2)
insert into tbl_课程表
values(003,'英语',3)
--学生表插入数据
insert into tbl_学生表
values(101,'马鹏磊')
insert into tbl_学生表
values(102,'张嘉龙')
--成绩表插入数据
insert into tbl_成绩表
values(101,1,100)
insert into tbl_成绩表
values(101,2,50)
insert into tbl_成绩表
values(101,3,60)
insert into tbl_成绩表
values(102,2,60)
insert into tbl_成绩表
values(102,3,50)
(1) 查询课程"001"成绩比课程"002"高的学生的姓名
--先查询课程002的成绩
--在查询成绩大于001
select st.name
from tbl_学生表 st,tbl_成绩表 sc
where st.id=sc.studentid
and courseid=001
and score>all(select score from tbl_成绩表 where courseid=002)
(2) 查询所有成绩都大于60分的学生姓名
--在学生表里面查询学生姓名 在成绩表里查询成绩小于60分的学生id
select st.name
from tbl_学生表 st
where st.id not in(select distinct sc.studentid from tbl_成绩表 sc where score<60)
(3) 查询所有"李老师"的课成绩大于60分的学生姓名
select st.name
from tbl_学生表 st, tbl_成绩表 sc,tbl_教师表 tt,tbl_课程表 so
where st.id=sc.studentid
and sc.courseid=tt.id
and so.teacherid=tt.id
and tt.name like '李%'
and sc.studentid not in(select score from tbl_成绩表 sc where score<60)
(4) 查询所有没选"李老师"的课的学生的姓名
--先选出来选了李老师课程的学生的名字
--再选出来没有选李老师课程的学生的姓名
select name
from tbl_学生表
where name not in
(select st.name
from tbl_学生表 st, tbl_成绩表 sc, tbl_教师表 tt, tbl_课程表 so
where st.id = sc.studentid
and sc.courseid = tt.id
and so.teacherid = tt.id
and tt.name like '李%')
(5) 查询平均成绩大于60的学生姓名
select st.name
from tbl_学生表 st,(select studentid from tbl_成绩表 group by studentid having avg(score)>60)g
where st.id=g.studentid
(4) 查询所有没选"李老师"的课的学生的姓名
select sname
from student,(select tid from teacher where tname not like '李%')t ,course,sc
where t.tid=course.tid and student.sid=sc.sid and course.cid=sc.cid
(5) 查询平均成绩大于60的学生姓名
select sname
from student ,(select sid from sc group by sid having avg(score)>60)g
where student.sid =g.sid
|