实验三四(答案)[一]
《实验三四(答案)[一]》由会员分享,可在线阅读,更多相关《实验三四(答案)[一](5页珍藏版)》请在装配图网上搜索。
1、实验三:数据库的嵌套查询实验实验目的:加深对嵌套查询语句的理解。实验内容:使用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 stud
2、ent where sdept = (select sdept from student where sname=刘晨) andsname刘晨V比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname=刘晨2. 查询选修了课程名为信息系统 的学生的学号和姓名:SQL Server中: select sno, sname from student where sno in(select sno from sc where cno in (select cno from course where
3、 cname=信息系统)VFP中: select sno, sname from student where sno in(select sno from sc, course where o=oand cname=信息系统)3. 查询选修了课程1和课程2的学生的学号:select sno from student where sno in (selectsnofrom sc where cno=1)and sno in (select sno from sc where cno=2)比较: 查询选修了课程1或课程2的学生的sno:select sno from sc where cno=1
4、or cno=2比较连接查询: select A.sno from sc A, sc B where A.sno=B.sno and A.cno=1 and B.cno=2 二. 使用带比较运算的子查询4. 查询比刘晨年龄小的所有学生的信息:select * from student where sage (select sage from student where sname=刘晨)三. 使用带Any, All谓词的子查询5. 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;select sname, sage from student where sage Any (sele
5、ct sage from student where sdept=IS) and sdeptIS6. 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:select sname, sage from student where sage ALL(select sage from student where sdept=IS) and sdeptIS7. 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄:select sno,sname,sage from student where sageall(select sage from student where sde
6、pt=CS)四. 使用带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 student where exists(select * from sc where sno=student.sno and cno=1)10. 查询没有选修了1号课程的学生
7、姓名:select sname from student where not exists(select * from sc where sno=student.sno and cno=1)11. 查询选修了全部课程的学生姓名:SQL Server中: select sname from student where not exists(select * from course where not exists( select * from sc where sno=student.sno and cno=o)11. 查询至少选修了学生95002选修的全部课程的学生的学号:SQL Server
8、中:select distinct sno from sc A where not exists (select * from sc B where sno=95002and not exists(select * from sc C where sno=A.sno and cno=B.cno)12. 求没有人选修的课程号cno和cnamecname:select cno,cname from course C where not exists(select * from sc where o=C.cno )13*. 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno
9、的课程SQL Server中: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 ) 思考:如何查询所有学生都选修了的课程的课程号cno?实验四:数据库的分组查询和统计查询实验目的:熟练掌握数据查询中的分组、统计、计算和集合
10、的操作方法。实验内容:使用聚集函数查询、分组计算查询、集合查询。实验步骤:一. 使用聚集函数:1 查询学生总人数:Select Count(*) as 学生总数 from student2. 查询选修了课程的学生总数:select count(distinct sno) as 选课学生总数 from sc3. 查询所有课程的总学分数和平均学分数,以及最高学分和最低学分:select sum(credit) as 总credit,avg(credit) as 课程平均学分,max(credit) as 最高学分,min(credit) as 最低学分 from course4. 计算1号课程的学
11、生的平均成绩, 最高分和最低分:select avg(grade) as 平均成绩,max(grade) as 最高分, min(grade) as 最低分from scwhere cno=15. 查询信息系(IS)学生”数据结构”课程的平均成绩:select avg(grade) from student, course, sc where student.sno=sc.sno ando=o and sdept=IS and cname=数据结构6*. 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade):select * from grade A where grade=(s
12、elect max(grade) from sc where sno=A.sno ) 7*. 求成绩低于该门课程平均成绩的学生的成绩信息(sno,cno,grade)select * from grade A where grade=(select avg(grade) from sc where cno=A.cno ) 二. 分组查询8. 查询各系的学生的人数并按人数从多到少排序 :selectsdept, Count(*) as 人数 from student group by sdept order by 人数 desc9. 查询各系的男女生学生总数, 并按系别,升序排列, 女生排在前:
13、select sdept,ssex,Count(*) as 人数 from student group by sdept, ssex order by sdept,ssex desc 10. 查询选修了3门课程已上的学生的学号和姓名:select sno, sname from student where sno in (select sno from sc group by (sno) having count(*)3) 11. 查询每个学生所选课程的平均成绩, 最高分, 最低分,和选课门数:select sno, avg(grade) as 平均成绩,max(grade) as 最高分,
14、min(grade) as 最低分, count(*) as 选课门数 from sc group by sno12. 查询至少选修了2门课程的学生的平均成绩:select sno, avg(grade) as 平均成绩, from sc group by sno having count(*)=213. 查询平均分超过80分的学生的学号和平均分:Select sno, avg(grade) as 平均成绩from sc group by sno having avg(*)=80比较: 求各学生的60分以上课程的平均分:select sno, avg(grade) as 平均成绩 from s
15、c where grade=60 group by sno14. 查询”信息系”(IS)中选修了5门课程以上的学生的学号:select sno from sc where sno in (select sno from student where sdept=IS) group by sno having count(*)=2三. 集合查询15. 查询数学系和信息系的学生的信息;select * from student where sdept=MA union select * from student where sdept=IS16. 查询选修了1号课程或2号课程的学生的学号:select sno from sc where cno=1 Union select sno from sc where cno=2比较实验三之3.思考:1. 用两种方法查询平均成绩少于70分的学生的学号。2*. 求各系的”大学英语”课程的成绩最高的学生的姓名和成绩。
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。