设有学生选取修课程数据库:
S(Sid, SNAME, AGE, SEX, DEPARTMENT, ADDRESS, BIRTHPLACE)
SC(Sid, Cid, GRADE)
C(Cid, CNAME, TEACHER)
(1) 李老师所教的课程号、课程名称;
select cid,cname
from c
where teacher = '李'
(2) 年龄大于23岁的女学生的学号和姓名;
select sid,sname
from s
where s.age>23 and sex = '女'
(3) “李小波”所选修的全部课程名称;
select c.cname
from c
where c.cid in (
select sc.cid
from s,sc
where s.sid = sc.sid and s.sname = '李小波'
);
(4) 所有成绩都在80分以上的学生姓名及所在系;
select sname,department
from s
where sid in (
select sid from sc group by sid having min(grade)>=80
);
(5) 没有选修“操作系统”课的学生的姓名;
select sname
from s
where s.sid not in (
select sc.sid from sc,c where sc.cid = c.cid and c.cname = '操作系统'
)
(6) 与“李小波”同乡的男生姓名及所在系;
select s.sname, s.department
from s
where s.sex='男'
and s.birthplace = (
select s.birthplace
from s
where s.sname='李小波'
);
(7) 英语成绩比数学成绩好的学生;
select s.sname from s where s.sid in(
select x.sid from sc x, sc y
where x.sid = y.sid
and x.grade>y.grade
and x.cid =(select cid from c where c.cname='英语')
and y.cid = (select cid from c where c.cname='数学')
);
(8) 选修同一门课程时,女生比男生成绩好的学生名单;
SELECT X1.SNAME
FROM S X1, SC Y1
WHERE X1.Sid=Y1.Sid AND
X1.SEX='女' AND
Y1.GRaDe>ALL
(SELECT Y2.GRaDe
FROM S X2, SC Y2
WHERE X2.Sid=Y2.Sid AND
X2.SEX='男' AND
Y2.Cid=Y1.Cid )
(9) 至少选修两门以上课程的学生姓名、性别;
select s.sname,s.sex
from s
where sid in (
select sid
from sc
group by sid
having count(*)>=2
)
(10) 选修了李老师所讲课程的学生人数;
select count(cid)
from sc
where sc.cid in(
select cid
from c
where c.teacher = '李老师'
)
(11) “操作系统”课程得最高分的学生姓名、性别、所在系;
select s.sname,s.sex,s.department from s,sc,c where s.sid= sc.sid and sc.cid = c.cid and c.cname ='操作系统' and sc.grade = (
select max(sc.grade)
from sc,c
where sc.cid = c.cid and c.cname = '操作系统' )