



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验四:数据库的嵌套查询实验学号: 姓名: 实验四:数据库的嵌套查询实验实验目的:加深对嵌套查询语句的理解。实验内容:使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。实验步骤:一. 使用带IN谓词的子查询1. 查询与刘晨在同一个系学习的学生的信息:select * from student where sdept in (select sdept from student where sname=刘晨)比较: select * from student where sdept = (select sdept from student where sname=刘晨) 的异同比较: select * from student where sdept = (select sdept from student where sname=刘晨) and sname刘晨比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname=刘晨结果:2. 查询选修了课程名为信息系统 的学生的学号和姓名:答:select sno, sname from studentwhere sno in(select sno from scwhere cno in(select cno from coursewhere cname = 信息系统)3. 查询选修了课程1和课程2的学生的学号(姓名):select sno from student where sno in (select sno from sc where cno=1)and sno in (select sno from sc where cno=2)select x.sno from SC x ,SC ywhere x.sno=y.sno and o=1 and o=2select sno from SC where cno=1 and sno in (select sno from SC where cno=2)比较: 查询选修了课程1或课程2的学生的sno:select sno from sc where cno=1 or cno=2比较连接查询: select A.sno from sc A, sc B where A.sno=B.sno and A.cno=1 and B.cno=2结果:第一类的查询结果为95001和95004;第二类查询结果有7种(包含重复);最后一类利用自连接查询结果有两种。二. 使用带比较运算的子查询4. 查询比刘晨年龄小的所有学生的信息:答:select * from studentwhere sage (select sage from student where sname = 刘晨 )三. 使用带Any, All谓词的子查询(对于ALL全称量词,建议改成否定之否定存在量词)5. 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;答:select sname, sagefrom studentwhere sdept IS andsage any (select sage from studentwhere sdept = IS)6. 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:答:select sname, sagefrom studentwhere sdept IS andsage all(select sage from studentwhere sdept = IS)7. 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄:答:select sno, sname, sagefrom studentwhere sage all(select sage from studentwhere sdept = IS)四. 使用带Exists谓词的子查询和相关子查询8. 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄:select sno,sname,sage from student A where not exists(select * from student B where A.sage=B.sage and A.snoB.sno) 9. 查询所有选修了1号课程的学生姓名:答:select sname from studentwhere exists(select * from scwhere sc.sno = student.sno and cno = 1)10. 查询没有选修了1号课程的学生姓名:答:select sname from studentwhere not exists(select * from scwhere sc.sno = student.sno and cno = 1)11. 查询选修了全部课程的学生姓名:答:select sname from studentwhere not exists(select * from coursewhere not exists (select * from scwhere sc.sno = student.sno and o = o)11. 查询至少选修了学生95002选修的全部课程的学生的学号:答:SELECT DISTINCT SNO FROM SC SCX WHERE NOT EXISTS (SELECT * FROM SC SCY WHERE SCY.SNO = 95002 AND NOT EXISTS (SELECT *FROM SC SCZ WHERE SCZ.SNO=SCX.SNO AND SCZ.CNO=SCY.CNO)12. 求没有人选修的课程号cno和cname:答:select cno,cnamefrom coursewhere not exists(select *from scwhere o = o)13*. 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno的课程select sno,cno from student,course where not exists(select * from sc where cno=o and sno=student.sno)14*. 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade):select * from sc A where grade=(select max(grade) from sc where sno=A.sno ) order by A.sno ASC (此处可否用GROUP by)select * from sc where grade in (select max(grade) from sc group by sno) (此答案有点问题: 当在数据库中同时有两个相同的最高分的,就出现判断错误.如95001里有一个最高分85, 95002里假如也有一个学生某门课成绩为85,这时问题就出现了.)select sc.sno, maxgrade from sc , (select sno, max(grade) as maxgrade from sc group by sno) as A where sc.sno=A.sno and grade=maxgrade 15. 如何查询所有学生都选修了的课程的课程号cno? 试用几种可能的方法实现。答:select cno from scgroup by cnohaving count(grade) = (sele
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届深圳市罗湖区高三开学质量检测语物理试题
- 民法总则试题及答案
- 专科生会计教学中的问题及对策思考-论文
- 承德法律知识培训报价课件
- 2025年涉电作业考试题及答案
- 医院锅炉项目
- 慢阻肺护理临床路径课件
- 2025年入学考试计算题及答案
- 护士病历操作考试题及答案
- 慕课不显示课件问题
- 孕产妇配偶艾滋病、梅毒检测服务流程
- IPV6组网技术 教案1 了解IPv6技术发展历史
- 教学用具AI应用行业深度调研及发展项目商业计划书
- 家居时代欧派家居的营销策略优化探讨
- 防泄漏及防腐蚀管理制度
- 工厂配餐合作协议书
- 和田县有机蔬果储藏冷库项目可行性报告赛孜古尔
- 高三生物一轮复习课件病毒 微专题
- 2025-2030男装市场市场现状供需分析及投资评估规划分析研究报告
- 航海概论航海技术臧继明课件
- 急性脑梗死静脉溶栓护理查房
评论
0/150
提交评论