SQL语句课堂练习题及答案.ppt_第1页
SQL语句课堂练习题及答案.ppt_第2页
SQL语句课堂练习题及答案.ppt_第3页
SQL语句课堂练习题及答案.ppt_第4页
SQL语句课堂练习题及答案.ppt_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

AnIntroductiontoDatabaseSystem,第三章综合练习,设有三个关系:S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher)试用SQL语句表示下列查询语句,3、查询学号为S3学生所学课程的课程名与任课教师名4、查询至少选修LIU老师所授课程中一门课程的女学生姓名5、查询WANG同学不学的课程的课程号6、查询至少选修两门课的学生学号7、查询全部学生都选修的课程的课程号与课程名8、查询选修课程包含LIU老师所授全部课程的学生学号。,1、查询LIU老师所授课程的课程号和课程名2、查询年龄大于23岁的男学生的学号和姓名,AnIntroductiontoDatabaseSystem,第三章综合练习,设有三个关系: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),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岁的男学生的学号和姓名,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岁的男学生的学号和姓名,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岁的男学生的学号和姓名,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,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,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),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=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*fromS,SCwhereS.sno=SC.snoandSC.cno=C.cnoandsname=WANG),涉及到全部的表:S,SC,C,方法一:NOTEXISTS嵌套查询,AnIntroductiontoDatabaseSystem,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,SelectcnofromCExceptSelectdistinctcnofromS,SCwhereS.sno=SC.snoandsname=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,AnIntroductiontoDatabaseSystem,综合练习答案,10、求LIU老师所授课程的每门课程的平均成绩,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),SelectC.cno,avg(grade)fromSC,CwhereSC.cno=C.cnoandteacher=LIUgroupbyC.cno,涉及到的表:SC,C,AnIntroductiontoDatabaseSystem,综合练习答案,11、检索姓名以L打头的所有学生的姓名和年龄,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Selectsname,agefromSwheresnamelikeL%,涉及到的表:S,AnIntroductiontoDatabaseSystem,综合练习答案,12、求年龄大于所有女同学年龄的男学生姓名和年龄,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Selectsname,agefromSwheresex=Mandageall(selectagefromSwheresex=F),涉及到的表:S,方法一:all,AnIntroductiontoDatabaseSystem,综合练习答案,12、求年龄大于所有女同学年龄的男学生姓名和年龄,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Selectsname,agefromSwheresex=Mandage(selectmax(age)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论