数据库的嵌套查询实验报告.docx
《数据库的嵌套查询实验报告.docx》由会员分享,可在线阅读,更多相关《数据库的嵌套查询实验报告.docx(14页珍藏版)》请在冰豆网上搜索。
数据库的嵌套查询实验报告
实验三:
数据库的嵌套查询实验
实验目的:
加深对嵌套查询语句的理解。
实验内容:
使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。
实验步骤:
一.使用带IN谓词的子查询
1.查询与’X晨’在同一个系学习的学生的信息:
比较select*fromstudentwheresdeptin
(selectsdeptfromstudentwheresname='X晨')
与:
select*fromstudentwheresdept=
(selectsdeptfromstudentwheresname='X晨')的异同
比较:
select*fromstudentwheresdept=
(selectsdeptfromstudentwheresname='X晨')andsname<>‘X晨’
与:
selectS1.*fromstudentS1,studentS2whereS1.sdept=S2.sdeptandS2.sname='X晨'的异同
2.查询选修了课程名为’信息系统’的学生的学号和某:
比较selectsno,snamefromstudentwheresnoin
(selectsnofromscwhereoin(selectofromcoursewhereame='信息系统'))
与:
selectsno,snamefromstudentwheresnoin
(selectsnofromsc,coursewheresc.o=course.oandame='信息系统')
3.查询选修了课程’1’和课程’2’的学生的学号:
selectsnofromstudentwheresnoin(selectsnofromscwhereo='1')
andsnoin(selectsnofromscwhereo='2')
比较:
查询选修了课程’1’或课程’2’的学生的sno:
selectsnofromscwhereo='1'oro='2'
比较连接查询:
selectA.snofromscA,scBwhereA.sno=B.snoandA.o='1'andB.o='2'
二.使用带比较运算的子查询
4.查询比’X晨’年龄小的所有学生的信息:
select*fromstudentwheresage<
(selectsagefromstudentwheresname='X晨')
三.使用带Any,All谓词的子查询
5.查询其他系中比信息系(IS)某一学生年龄小的学生某和年龄;
selectsname,sagefromstudentwheresage(selectsagefromstudentwheresdept='IS')andsdept<>'IS'
6.查询其他系中比信息系(IS)学生年龄都小的学生某和年龄:
selectsname,sagefromstudentwheresage(selectsagefromstudentwheresdept='IS')andsdept<>'IS'
7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号,某和年龄:
selectsno,sname,sagefromstudentwheresage<>all
(selectsagefromstudentwheresdept='CS')
四.使用带Exists谓词的子查询和相关子查询
8.查询与其他所有学生年龄均不同的学生学号,某和年龄:
selectsno,sname,sagefromstudentAwherenotexists
(select*fromstudentBwhereA.sage=B.sageandA.sno<>B.sno)
9.查询所有选修了1号课程的学生某:
selectsnamefromstudentwhereexists
(select*fromscwheresno=student.snoando='1')
10.查询没有选修了1号课程的学生某:
selectsnamefromstudentwherenotexists
(select*fromscwheresno=student.snoando='1')
11.查询选修了全部课程的学生某:
SQLServer中:
selectsnamefromstudentwherenotexists
(select*fromcoursewherenotexists
(select*fromscwheresno=student.snoando=course.o))
11.查询至少选修了学生95002选修的全部课程的学生的学号:
SQLServer中:
selectdistinctsnofromscAwherenotexists
(select*fromscBwheresno='95002'andnotexists
(select*fromscCwheresno=A.snoando=B.o))
12.求没有人选修的课程号o和ameame:
selecto,amefromcourseCwherenotexists
(select*fromscwheresc.o=C.o)
13*.查询满足条件的(sno,o)对,其中该学号的学生没有选修该课程号o的课程
SQLServer中:
selectsno,ofromstudent,coursewherenotexists
(select*fromscwhereo=course.oandsno=student.sno)
14*.查询每个学生的课程成绩最高的成绩信息(sno,o,grade):
select*fromscAwheregrade=
(selectmax(grade)fromscwheresno=A.sno)
思考:
如何查询所有学生都选修了的课程的课程号o?
selecto
fromsc
groupbyo
havingcount(*)=(selectcount(*)fromstudent)