已阅读5页,还剩27页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
-,第三章综合练习,设有三个关系:S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher)试用SQL语句表示下列查询语句,10、求LIU老师所授课程的每门课程的平均成绩11、检索姓名以L打头的所有学生的姓名和年龄。12、求年龄大于所有女同学年龄的男学生姓名和年龄。13、往关系C中插一个课程元组(C8,VC+,BAO)14、把选修LIU老师课程的女同学选课元组全部删去。15、把低于所有课程总平均成绩的男同学成绩提高5%.,9、统计每门课程的学生选修人数(超过10人的课程才统计)。要求显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),1、查询LIU老师所授课程的课程号和课程名,方法一:Selectcno,cnamefromCwhereteacher=LIU,涉及到的表:C(cno,cname,teacher),方法二:LIKESelectcno,cnamefromCwhereteacherlikeLIU,方法三:INSelectcno,cnamefromCwhereteacherin(LIU),-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsno,snamefromSwhereage23andsex=M,涉及到的表:S(sno,sname,sex,age),方法一:一般的查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsno,snamefromSwhereage23andsnoin(selectsnofromswheresex=男),涉及到的表:S(sno,sname,sex,age),方法二:用IN嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsno,snamefromSwhereage23andsno=ANY(selectsnofromswheresex=男),涉及到的表:S(sno,sname,sex,age),方法三:用=ANY嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsx.sno,sx.snamefromssx,sxywheresx.sno=sy.snoandsx.age23andsy.sex=男,涉及到的表:Sx(sno,sname,sex,age),方法四:自连接,涉及到的表:Sy(sno,sname,sex,age),-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsx.sno,sx.snamefromssxinnerjoinsxyonsx.sno=sy.snowheresx.age23andsy.sex=男,涉及到的表:Sx(sno,sname,sex,age),方法五:内连接,涉及到的表:Sy(sno,sname,sex,age),-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsx.sno,sx.snamefromssxwheresx.age23andexists(select*fromssywheresy.sex=男andsy.sno=sx.sno),涉及到的表:S(sno,sname,sex,age),方法六:用EXISTS嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Selectsno,snamefromSwhereage23IntersectSelectsno,snamefromSwheresex=男,涉及到的表:S(sno,sname,sex,age),方法七:集合查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Selectcname,teacherfromSC,CwhereSC.cno=C.cnoandsno=S3,涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法一:自然连接查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Selectcname,teacherFromSCinnerjoinCOnSC.cno=C.cnowheresno=S3,涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法二:内连接查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Selectcname,teacherfromCwherecnoin(selectcnofromSCwheresno=S3),涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法三:IN嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Selectcname,teacherfromCwherecno=ANY(selectcnofromSCwheresno=S3),涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法四:=ANY嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Selectcname,teacherfromCwhereexists(select*fromSCwheresno=S3andSC.cno=C.cno),涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法五:EXIST嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnamefromS,SC,CwhereS.sno=SC.snoandSC.cno=C.cnoandsex=Fandteacher=LIU,方法一:自然连接查询,涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnamefromSinnerjoinSCOnS.sno=SC.snoinnerjoinConSC.cno=C.cnowheresex=Fandteacher=LIU,方法二:内连接查询,涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnamefromSwheresex=Fandsnoin(selectsnofromSCwherecnoin(selectcnofromCwhereteacher=LIU),方法三:IN嵌套查询,涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnamefromSwheresex=FandsnoANY(selectsnofromSCwherecno=ANY(selectcnofromCwhereteacher=LIU),方法四:ANY嵌套查询,涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnamefromSwheresex=Fandexists(select*fromCwhereteacher=LIUandexists(select*fromSCwhereSC.sno=S.snoandSC.cno=C.cno),方法五:EXISTS嵌套查询,涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCwherenotexists(select*fromSwheresname=WANGandexistsselect*fromSCwheresno=SC.snoandcno=C.cno),涉及到全部的表:S,SC,C,方法一:NOTEXISTS嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCWherecnonotin(selectcnofromSCwheresnoin(selectsnofromSwheresname=WANG),涉及到全部的表:S,SC,C,方法二:NOTIN嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCWherecnoall(SelectcnofromSCwheresno=any(selectsnofromSwheresname=WANG),涉及到全部的表:S,SC,C,方法三:ANY/ALL查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCMinusSelectdistinctcnofromS,SCwhereS.sno=SC.snoandsname=WANG,涉及到全部的表:S,SC,C,方法四:集合查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCwherenotexists(select*fromS,SCwhereS.sno=SC.snoandSC.cno=C.cnoandsname=WANG),涉及到全部的表:S,SC,C,方法五:混合嵌套查询,-,AnIntroductiontoDatabaseSystem,综合练习答案,6、查询至少选修两门课的学生学号,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectsnofromSCgroupbysnohavingcount(*)=2,涉及到的表:SC,-,AnIntroductiontoDatabaseSystem,综合练习答案,7、查询全部学生都选修的课程的课程号与课程名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Selectcno,cnamefromCwherenotexists(select*fromSwherenotexists(select*fromSCwheresno=S.snoandcno=C.cno),涉及到全部的表:S,SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,8、查询选修课程包含LIU老师所授全部课程的学生学号,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectdistinctsnofromSCasXwherenotexists(select*fromCwhereteacher=LIUandnotexists(select*fromSCasYwhereY.sno=X.snoandY.cno=C.cno),涉及到的表:SC,C,-,AnIntroductiontoDatabaseSystem,综合练习答案,9、统计每门课程的学生选修人数(超过10人的课程才统计)。要求显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Selectcno,count(sno)fromSCgroupbycnohavingcount(*)10orderby2desc,1,涉及到的表:SC,-,AnIntroductiontoDatabaseS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上海工程技术大学《安全法规》2025-2026学年第一学期期末试卷(A卷)
- 上海工程技术大学《Android 移动应用开发》2025-2026学年第一学期期末试卷(A卷)
- 上海工商职业技术学院《安全系统工程》2025-2026学年第一学期期末试卷(B卷)
- 上海工商职业技术学院《安全原理》2025-2026学年第一学期期末试卷(B卷)
- 欣赏 管弦乐曲《马刀舞曲》说课稿2025学年小学音乐花城版六年级下册-花城版
- 胫骨骨折的康复护理法律法规
- 上饶卫生健康职业学院《安全检测技术》2025-2026学年第一学期期末试卷(B卷)
- 上饶卫生健康职业学院《Android 移动应用开发》2025-2026学年第一学期期末试卷(A卷)
- 上海音乐学院《安全管理》2025-2026学年第一学期期末试卷(B卷)
- 26年检测职称考试考点梳理
- 乙烯装置培训课件
- 雨课堂在线学堂《医学实验技术与方法新进展》单元考核测试答案
- 雨课堂在线学堂《现代礼仪》作业单元考核答案
- 信托业务部门绩效考核方案
- 2026年国家电网招聘之电网计算机考试题库500道及一套参考答案
- 《水利水电工程柔性测斜仪安装与监测规程》
- 厨卫电器授权合同范本
- 义齿技术服务合同范本
- 触摸屏基础知识课件
- 材料成形基本原理第三版祖方遒课后习题答案
- 2025年保安证考试100题及答案
评论
0/150
提交评论