《课堂综合练习题答案.ppt》由会员分享,可在线阅读,更多相关《课堂综合练习题答案.ppt(27页珍藏版)》请在三一办公上搜索。
1、An Introduction to Database System,第三章 综合练习,设有三个关系: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岁的男学生的学
2、号和姓名,An Introduction to Database System,第三章 综合练习,设有三个关系: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
3、人的课程才统计)。要求显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),1、查询LIU老师所授课程的课程号和课程名,Select cno,cname from Cwhere teacher=LIU,涉及到的表:C(cno,cname,teacher),An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)
4、SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sno,sname from S where age23 and sex=M,涉及到的表:S(sno,sname,sex,age),方法一:一般的查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sno,sname from S where age23
5、 and sno in(select sno from s where sex=男),涉及到的表:S(sno,sname,sex,age),方法二:用IN嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sx.sno,sx.sname from s sx where sx.age23 and exists(select*from s sy where sy.sex=男 and s
6、y.sno=sx.sno),涉及到的表:S(sno,sname,sex,age),方法三:用EXISTS嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sx.sno,sx.sname from s sx,s xywhere sx.sno=sy.sno andsx.age23 and sy.sex=男,涉及到的表:Sx(sno,sname,sex,age),方法四:自连接,涉及到
7、的表:Sy(sno,sname,sex,age),An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sno,sname from S where age23 IntersectSelect sno,sname from S where sex=男,涉及到的表:S(sno,sname,sex,age),方法五:集合查询,An Introduction to Database System,综
8、合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Select cname,teacher from SC,C where SC.cno=C.cno and sno=S3,涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法一:连接查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teach
9、er),3、查询学号为S3学生所学课程的课程名与任课教师名,Select cname,teacher from C where cno in(select cno from SC where sno=S3),涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法二:IN嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Select cname,te
10、acher from C where exists(select*from SC where sno=S3 and SC.cno=C.cno),涉及到的表:SC(sno,cno,grade)C(cno,cname,teacher),方法三:EXIST嵌套查询,An Introduction to Database System,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select sname from S,SC,C where S.sno=SC.s
11、no and SC.cno=C.cno and sex=F and teacher=LIU,方法一:连接查询,涉及到全部的表:S,SC,C,An Introduction to Database System,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select sname from Swhere sex=F and sno in(select sno from SC where cno in(select cno from C where tea
12、cher=LIU),方法二:IN嵌套查询,涉及到全部的表:S,SC,C,An Introduction to Database System,综合练习答案,4、查询至少选修LIU老师所授课程中一门课程的女学生姓名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select sname from Swhere sex=F and exists(select*from C where teacher=LIU and exists(select*from SC where SC.sno=S.sno and SC.cno=C.cno
13、),方法三:EXISTS嵌套查询,涉及到全部的表:S,SC,C,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,Select cno from Cwhere not exists(select*from S,SC where S.sno=SC.sno and SC.cno=C.cno and sname=WANG),涉及到全部的表:S,SC,C,方法一:NOT EXISTS嵌套查询,An Introduc
14、tion to Database System,综合练习答案,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,Select cno from CExceptSelect distinct cno from S,SC where S.sno=SC.sno and sname=WANG,涉及到全部的表:S,SC,C,方法二:集合查询,An Introduction to Database System,综合练习答案,6、查询至少选修两门课的学生学号,S(sno,sname,sex,age)S
15、C(sno,cno,grade)C(cno,cname,teacher),Select sno from SCgroup by sno having count(*)=2,涉及到的表:SC,An Introduction to Database System,综合练习答案,7、查询全部学生都选修的课程的课程号与课程名,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select cno,cname from Cwhere not exists(select*from S where not exists(select*from
16、 SC where sno=S.sno and cno=C.cno),涉及到全部的表:S,SC,C,An Introduction to Database System,综合练习答案,8、查询选修课程包含LIU老师所授全部课程的学生学号,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select distinct sno from SC as Xwhere not exists(select*from C where teacher=LIU and not exists(select*from SC as Y where Y
17、.sno=X.sno and Y.cno=C.cno),涉及到的表:SC,C,An Introduction to Database System,综合练习答案,9、统计每门课程的学生选修人数(超过10人的课程才统计)。要求显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select cno,count(sno)from SC group by cno having count(*)10order by 2 desc,1,涉及到的表:SC,An Intro
18、duction to Database System,综合练习答案,10、求LIU老师所授课程的每门课程的平均成绩,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select C.cno,avg(grade)from SC,Cwhere SC.cno=C.cno and teacher=LIU group by C.cno,涉及到的表:SC,C,An Introduction to Database System,综合练习答案,11、检索姓名以L打头的所有学生的姓名和年龄,S(sno,sname,sex,age)SC(sno
19、,cno,grade)C(cno,cname,teacher),Select sname,age from S where sname like L%,涉及到的表:S,An Introduction to Database System,综合练习答案,12、求年龄大于所有女同学年龄的男学生姓名和年龄,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select sname,age from Swhere sex=M and ageall(select age from S where sex=F),涉及到的表:S,方法一:al
20、l,An Introduction to Database System,综合练习答案,12、求年龄大于所有女同学年龄的男学生姓名和年龄,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Select sname,age from Swhere sex=M and age(select max(age)from S where sex=F),涉及到的表:S,方法二:max,An Introduction to Database System,综合练习答案,13、往关系C中插一个课程元组(C8,VC+,BAO),S(sno,sna
21、me,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Insert into C values(C8,VC+,BAO),An Introduction to Database System,综合练习答案,14、把选修LIU老师课程的女同学选课元组全部删去,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Delete from SC where sno in(select sno from S where sex=F)and cno in(select cno from C where teacher=LIU),An Introduction to Database System,综合练习答案,15、把低于所有课程总平均成绩的男同学成绩提高5%,S(sno,sname,sex,age)SC(sno,cno,grade)C(cno,cname,teacher),Update SC set grade=grade*1.05 where sno in(select sno from S where sex=F)and grade(select avg(grade)from SC),
链接地址:https://www.31ppt.com/p-6377033.html