付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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、查询年龄大
2、于23岁的男学生的学号和姓名,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% .,
3、9、统计每门课程的学生选修人数(超过10人的课程才统计)。要求显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),1、查询LIU老师所授课程的课程号和课程名,方法一: Select cno,cname from C where teacher=LIU,涉及到的表: C(cno,cname,teacher),方法二:LIKE Select cno,cname from C
4、where teacher like LIU,方法三:IN Select cno,cname from C where teacher in (LIU),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 and sex=M,涉及到的表:S(sno,sname,sex,age),方法一:一般的查询,An Introduction
5、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 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
6、,grade) C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Select sno,sname from S where age23 and sno =ANY (select sno from s where sex=男),涉及到的表: S(sno,sname,sex,age),方法三:用=ANY嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),2、查询年龄大于23岁的男学生的学号和姓名,Se
7、lect sx.sno, sx.sname from s sx, s xy where sx.sno=sy.sno and sx.age23 and sy.sex=男,涉及到的表: Sx(sno,sname,sex,age),方法四:自连接,涉及到的表: 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 sx.sno, sx.sname fr
8、om s sx inner join s xy on sx.sno=sy.sno where sx.age23 and sy.sex=男,涉及到的表: Sx(sno,sname,sex,age),方法五:内连接,涉及到的表: 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 sx.sno, sx.sname from s sx where s
9、x.age23 and exists (select * from s sy where sy.sex=男 and sy.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 sno,sname from S where age23 Intersect Select sno,sn
10、ame from S where sex=男,涉及到的表: S(sno,sname,sex,age),方法七:集合查询,An Introduction to Database System,综合练习答案,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),方法一:
11、自然连接查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Select cname ,teacher From SC inner join C On SC.cno=C.cno where sno=S3,涉及到的表: SC(sno,cno,grade) C(cno,cname,teacher),方法二:内连接查询,An Introduction to Database System,综合
12、练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),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,
13、grade) C(cno,cname,teacher),3、查询学号为S3学生所学课程的课程名与任课教师名,Select cname ,teacher from C where cno =ANY ( select cno from SC where sno=S3 ),涉及到的表: SC(sno,cno,grade) C(cno,cname,teacher),方法四:=ANY嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),3、查询学
14、号为S3学生所学课程的课程名与任课教师名,Select cname ,teacher 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,teache
15、r),Select sname from S,SC,C where S.sno=SC.sno 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 S inner join SC On S.sno=SC.sno inn
16、er join C on SC.cno=C.cno where 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 S where sex=F and sno in (select sno from SC where cno in (select cno fr
17、om C where teacher=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 S where sex=F and sno ANY (select sno from SC where cno =ANY (select cno from C where teacher=LIU),方法
18、四:ANY嵌套查询,涉及到全部的表: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 S where 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 )
19、,方法五: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 C where not exists (select * from S where sname=WANG and exists select * from SC where sno=SC.sno and cno=C.cno ),涉及到全部的表:S,SC,C,方法
20、一:NOT EXISTS嵌套查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,Select cno from C Where cno not in (select cno from SC where sno in (select sno from S where sname=WANG),涉及到全部的表:S,SC,C,方法二:NOT IN嵌套查询,An Introduction to Databas
21、e System,综合练习答案,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,Select cno from C Where cno all (Select cno from SC where sno =any (select sno from S where sname=WANG),涉及到全部的表:S,SC,C,方法三:ANY/ALL查询,An Introduction to Database System,综合练习答案,S(sno,sname,sex,age) SC(sno,c
22、no,grade) C(cno,cname,teacher),5、查询WANG同学不学的课程的课程号,Select cno from C Minus Select distinct cno from S, SC where S.sno=SC.sno and sname=WANG,涉及到全部的表: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 fro
23、m C where not exists (select * from S,SC where S.sno=SC.sno and SC.cno=C.cno and sname=WANG),涉及到全部的表:S,SC,C,方法五:混合嵌套查询,An Introduction to Database System,综合练习答案,6、查询至少选修两门课的学生学号,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),Select sno from SC group by sno having count(*)=2,涉及到的表:SC,An
24、Introduction to Database System,综合练习答案,7、查询全部学生都选修的课程的课程号与课程名,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),Select cno,cname from C where not exists (select * from S where not exists (select * from SC where sno=S.sno and cno=C.cno),涉及到全部的表:S,SC,C,An Introduction to Database System,综合练习
25、答案,8、查询选修课程包含LIU老师所授全部课程的学生学号,S(sno,sname,sex,age) SC(sno,cno,grade) C(cno,cname,teacher),Select distinct sno from SC as X where not exists (select * from C where teacher=LIU and not exists (select * from SC as Y where Y.sno=X.sno and Y.cno=C.cno),涉及到的表:SC,C,An Introduction to Database System,综合练习答案
26、,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(*)10 order by 2 desc,1,涉及到的表:SC,An Introduction to Database System,综合练习答案,10、求LIU老师所授课程的每门课程的平均成绩,S(sno,sname,sex,ag
27、e) SC(sno,cno,grade) C(cno,cname,teacher),Select C.cno,avg(grade) from SC,C where 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,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 S where sex=M and a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中西科工作制度
- 六无村工作制度
- 保全工作制度
- 一查工作制度
- 全友工作制度
- 医疗营销推广方案
- 安全骑行带头盔
- 清扫保洁安全培训
- 创业融资商业计划书-蓝色-科技风
- 台湾省工作制度
- 盘点:2026年AI智能CRM系统主流品牌
- 2026年发展对象党章测试题及答案
- 2026年宁夏葡萄酒与防沙治沙职业技术学院单招职业技能测试题库及答案详解(夺冠)
- 2026年阜阳职业技术学院单招职业技能测试题库附参考答案详解(能力提升)
- 装配式工程质量标准化管理手册
- DB42-T 2509-2026 数字乡村 地质资源信息化建设与应用规范
- 财税销售技巧培训课件
- GB/T 46894-2025车辆集成电路电磁兼容试验通用规范
- T∕CNCA 127-2025 煤炭建设工程造价参考指标
- 2026春统编版二年级下册道德与法治第四单元教学设计
- 粉末冶金培训课件
评论
0/150
提交评论