sql数据库的嵌套查询

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验四:数据库的嵌套查询实验学号:姓名:实验四:数据库的嵌套查询实验实验目的:加深对嵌套查询语句的理解。实验内容:使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。实验步骤:一.使用带IN谓词的子查询1.查询与’刘晨’在同一个系学习的学生的信息:select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname='刘晨')比较:select*fromstudentwheresdept=(selectsdeptfromstudentwheresname='刘晨')的异同比较:select*fromstudentwheresdept=(selectsdeptfromstudentwheresname='刘晨')andsname'刘晨V比较:selectS1.*fromstudentS1,studentS2whereS1.sdept=S2.sdeptandS2.sname='刘晨'2.查询选修了课程名为’信息系统’的学生的学号和姓名:SQLServer中:selectsno,snamefromstudentwheresnoin(selectsnofromscwherecnoin(selectcnofromcoursewherecname='信息系统'))3.查询选修了课程’1’和课程’2’的学生的学号(姓名):selectsnofromstudentwheresnoin(selectsnofromscwherecno='1')andsnoin(selectsnofromscwherecno='2')selectx.snofromSCx,SCywherex.sno=y.snoandx.cno='1'andy.cno='2'selectsnofromSCwherecno='1'andsnoin(selectsnofromSCwherecno='2')比较:查询选修了课程’1’或课程’2’的学生的sno:selectsnofromscwherecno='1'orcno='2'比较连接查询:selectA.snofromscA,scBwhereA.sno=B.snoandA.cno='1'andB.cno='2'二.使用带比较运算的子查询4.查询比’刘晨’年龄小的所有学生的信息:select*fromstudentwheresage(selectsagefromstudentwheresname='刘晨')三.使用带Any,All谓词的子查询(对于ALL全称量词,建议改成否定之否定存在量词)5.查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;selectsname,sagefromstudentwheresageAny(selectsagefromstudentwheresdept='IS')andsdept'IS'6.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:selectsname,sagefromstudentwheresageALL(selectsagefromstudentwheresdept='IS')andsdept'IS'7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentwheresageall(selectsagefromstudentwheresdept='CS')四.使用带Exists谓词的子查询和相关子查询8.查询与其他所有学生年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentAwherenotexists(select*fromstudentBwhereA.sage=B.sageandA.snoB.sno)9.查询所有选修了1号课程的学生姓名:selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1')selectsnamefromstudentwheresnoin(selectsnofromscwherecno='1')10.查询没有选修了1号课程的学生姓名:selectsnamefromstudentwherenotexists(select*fromscwheresno=student.snoandcno='1')11.查询选修了全部课程的学生姓名:selectsnamefromstudentwherenotexists不存在一门课没有选的学生.(select*fromcoursewherenotexists一门课都没有选的,(select*fromscwheresno=student.snoandcno=course.cno))选某门课,11.查询至少选修了学生95002选修的全部课程的学生的学号:selectdistinctsnofromscAwherenotexists(select*fromscBwheresno='95002'andnotexists(select*fromscCwheresno=A.snoandcno=B.cno))12.求没有人选修的课程号cno和cname:selectcno,cnamefromcourseCwherenotexists(select*fromscwheresc.cno=C.cno)13*.查询满足条件的(sno,cno)对,其中该学号的学生没有选修该课程号cno的课程selectsno,cnofromstudent,coursewherenotexists(select*fromscwherecno=course.cnoandsno=student.sno)14*.查询每个学生的课程成绩最高的成绩信息(sno,cno,grade):select*fromscAwheregrade=(selectmax(grade)fromscwheresno=A.sno)orderbyA.snoASC(此处可否用GROUPby)select*fromscwheregradein(selectmax(grade)fromscgroupbysno)(此答案有点问题:当在数据库中同时有两个相同的最高分的,就出现判断错误.如95001里有一个最高分85,95002里假如也有一个学生某门课成绩为85,这时问题就出现了.)selectsc.sno,maxgradefromsc,(selectsno,max(grade)asmaxgradefromscgroupbysno)asAwheresc.sno=A.snoandgrade=maxgrade思考:如何查询所有学生都选修了的课程的课程号cno?试用几种可能的方法实现。

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功