數據庫系統原理與應用-多表查詢
1.查詢沒有選修1號課程的學生姓名。
SELECT student.sname
FROM student
WHERE NOT EXISTS
(SELECT *
FROM sc
WHERE student.sno=sc.sno AND cno='1');
2.查詢所有選修了1號課程的學生姓名
select student.sname
from student
where student.sno=(
select sc.sno
from sc
where
sc.sno=student.sno and sc.cno ='1');
3.查詢非計算機科學系中比計算機科學系所有學生年齡都小的學生姓名及年齡。
select 姓名,年齡 from 表 where 年齡<(select min(年齡) from 表 where 系=CS系)
select sname,sage
from student
where sage < all
(select sage
from student
where sdept='cs'
)
4.查詢非計算機科學系中比計算機科學系任意一個學生年齡小的學生姓名和年齡。
select sname,sage
from student
where sage<any(
select sage from student where sdept='cs')
and sdept<>'cs'
5.找出每個學生超過他選修課程平均成績的課程號。
select sno,cno
from sc sc1
where grade>(
select avg(grade)
from sc sc2
where sc1.sno=sc2.sno)
6.查詢選修了課程名爲“信息系統”的學生學號和姓名。
select sno,sname
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course
where cname='信息系統'
)
);
7.查詢與“劉晨”在同一個系學習的學生(學號,姓名,系)。
select sno,sname,sdept
from student
where sdept in
(select sdept
from student
where sname='劉晨');
8.查詢選修了全部課程的學生姓名。
select sname
from student
where not exists
(select * from course
where not exists
(select * from sc
where sno=student.sno
and cno=course.cno));
9.查詢每個學生及其選修課程的情況,保留學生學號,姓名,課程號,成績。
select student.sno,student.sname,sc.cno,sc.grade
from student,sc
where student.sno=sc.sno;
10.查詢選修了1號課程的學生及其選修課程的情況,保留學生學號,姓名,課程名,成績。
select sc.sno,student.sname,course.cname,sc.grade
from student,course,sc
where student.sno=sc.sno and course.cno='1'and course.cno=sc.cno;
11.查詢每個學生及其選修課程的情況,保留學生學號,姓名,課程號,課程名,成績。
select student.sno,student.sname,sc.cno,course.cname,sc.grade
from student,sc,course
where student.sno=sc.sno and sc.cno = course.cno;
12.查詢每一門課的間接先修課(即先修課的先修課),保留課程號,先修課的課程號。
select first.cno,second.cpno
from course first,course second
where first.cpno =second.cno;