34实 验 三四Word格式文档下载.docx
- 文档编号:17183604
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:21
- 大小:19.06KB
34实 验 三四Word格式文档下载.docx
《34实 验 三四Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《34实 验 三四Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。
例2.19查询选修了C03号课程的学生的学号(Sno)和成绩(Grade),并按成绩降序排列。
例2.20查询全体学生情况,查询结果按所在系的系名(Sdpet)升序排列,同一系中的学生按年龄(Sage)降序排列。
4集函数的使用
例2.21查询学生总人数。
例2.22查询选修了课程的学生人数。
例2.23计算选修C01号课程的学生平均成绩。
例2.24查询选修C01号课程的学生最高分数。
5查询结果分组
例2.25求各个课程号(Cno)及相应的选课人数。
例2.26查询选修了3门或3门以上课程的学生学号(Sno)。
B、连接查询
1不同表之间的连接查询
例2.27查询每个学生及其选修课程的情况。
例2.28查询每个学生的学号(Sno)、姓名(Sname)、选修的课程名(Cname)及成绩(Grade)。
2自身连接
例2.29查`询每一门课的间接先修课(即先修课的先修课)。
3外连接
例2.30把例3.37中的等值连接改为左连接。
C、嵌套查询
1带谓词IN的嵌套查询
例2.31查询选修了编号为“C02”的课程的学生姓名(Sname)和所在系(Sdept)。
例2.32查询与“李伟”在同一个系学习的学生学号(Sno)、姓名(Sname)和系名(Sdept)。
例2.33查询选修了课程名为“数据结构”的学生学号(Sno)和姓名(Sname)。
2带有比较运算符的嵌套查询
例2.34将例3.42改为带有比较运算符的嵌套查询。
3带谓词ANY或ALL的嵌套查询
例2.35查询非自动化系的不超过自动化系所有学生的年龄的学生姓名(Sname)和年龄(Sage)。
4带谓词EXISTS的嵌套查询
例2.36查询所有选修了编号为“C01”课程的学生姓名(Sname)和所在系(Sdept)。
例2.37将例3.42改为带谓词EXISTS的查询,其SQL语句如下
例2.38查询选修了所有课程的学生姓名(Sname)和所在系。
D、集合查询
例2.39查询计算机科学系的学生或年龄不大于20岁的学生信息。
例2.40查询数学系的学生且年龄不大于20岁的学生的交集,这实际上就是查询数学系中年龄不大于20岁的学生。
例2.41查询数学系的学生与年龄不大于20岁的学生的差集。
E、综合应用(要求不少于两种实现方法)
绩的学生姓名、课程名、本课程成绩。
selectSname,Sno,Sdeptfromsystem.student;
selectSname,2012-Sageas出生年月,Snofromsystem.student;
selectSname,2012-Sageas出生年月,LOWER(Sno)fromsystem.student;
selectdistinctsc.snofromsystem.sc;
selectsno,snamefromsystem.studentwheresdept='
计科系'
;
selectsname,sagefromsystem.studentwheresagebetween18and22;
selectsname,sagefromsystem.studentwheresagenotbetween18and22;
selectSno,Sname,Ssexfromsystem.studentwheresdeptin('
'
电气学院'
数理学院'
);
selectSname,Sno,Ssexfromsystem.studentwhereSnamelike'
刘%'
selectSname,Sdeptfromsystem.studentwhereSnameLike'
刘___'
selectSname,Sno,Ssexfromsystem.studentwhereSnamenotlike'
selectCno,Ccreditfromsystem.coursewhereCnamelike'
DB\_设计'
escape'
\'
selectCno,Ccreditfromsystem.coursewhereCnamelike'
DB\_%i__'
escape'
selectsno,cnofromsystem.scwheregradeisnull;
selectsno,cnofromsystem.scwheregradeisnotnull;
selectsno,gradefromsystem.scwherecno='
C03'
orderbygradedesc;
select*fromsystem.studentorderbysdept,sagedesc;
selectcount(*)fromsystem.student;
selectcount(distinctsno)fromsystem.sc;
selectavg(grade)fromsystem.scwherecno='
C01'
selectmax(grade)fromsystem.scwherecno='
selectcno,count(sno)fromsystem.scgroupbycno;
selectsnofromsystem.scgroupbysnohavingcount(*)>
=3;
selectstudent.*,sc.*fromsystem.student,system.scwherestudent.sno=sc.sno;
selectstudent.sno,sname,cname,gradefromsystem.sc,system.student,system.coursewherestudent.sno=sc.snoando=o;
selecto,second.cpnofromsystem.coursefirst,system.coursesecondwherefirst.cpno=o;
selectstudent.sno,sname,ssex,sage,sdept,cno,gradefromsystem.studentleftouterjoinsystem.scon(student.sno=sc.sno);
selectstudent.sno,sname,ssex,sage,sdept,cno,gradefromsystem.studentleftouterjoinsystem.scusing(sno);
1.
selectSname,Sdept
fromsystem.student
whereSnoin
(selectSno
fromsystem.sc
wherecno='
C02'
selectSno,Sname,Sdept
whereSdeptin
(selectSdept
fromsystem.student
whereSname='
刘亚平'
selectSno,Sname
whereCnoin
(selectCno
fromsystem.course
whereCname='
数据结构'
)
)
或者:
selectstudent.Sno,Sname
fromsystem.student,system.sc,system.course
wherestudent.Sno=sc.Snoand
sc.Cno=course.Cnoand
course.Cname='
2.
whereSdept=
3.
selectSname,Sage
whereSage<
=all
(selectSage
whereSdept='
andSdept<
>
'
4.
whereexists
(select*
whereSno=student.SnoandCno='
fromsystem.studentS1
fromsystem.studentS2
whereS2.Sdept=S1.Sdeptand
S2.Sname='
fromsystem.Student
wherenotexists
wherenotexists
whereSno=student.Sno
andCno=course.Cno));
select*
whereSdept='
union
=20;
intersect
and
Sage<
minus
E.综合应用
selectsnoas学号,count(*)as课程门数,sum(ccredit)
fromsystem.sc,system.course
whereo=o
groupbysno;
selectsnameas姓名
fromsystem.student,system.sc
wherestudent.sno=sc.sno
groupbystudent.sname
having(avg(grade)>
(selectavg(grade)
wheresno='
200400812140'
groupbysno
);
selectsnoas学号,avg(grade)as平均成绩,count(*)as课程门数
fromsystem.sc
5.
selectstudent.snoas学号,avg(grade)as平均成绩,count(*)as课程门数
fromsystem.sc,system.student
wherestudent.sno=sc.snoand
sdeptin
(selectsdept
wheresname='
刘春香'
groupbystudent.sno;
6.
selectstudent.snoas学号,snameas姓名,avg(grade)as平均成绩
wherestudent.sno=sc.sno
groupbystudent.sno,sname
havingavg(grade)>
60
orderby平均成绩desc;
7.
selectsnameas姓名,count(*)as课程门数,avg(grade)as平均成绩
sname='
groupbysname;
8.
fromsystem.student,system.sc
wherestudent.sno=sc.snoandsname='
李大鹏'
groupbysname
方法2
selectavg(grade),count(sc.sno)
fromsc,student
wheresdeptin(selectsdept
fromstudent
andsc.sno=student.sno
groupbysc.sno;
例6、查询平均成绩大于60分的所有学生的平均成绩,要求打印出学生的“学号”、“姓名”和“平均成绩”,并按平均成绩降序排列。
方法1
selectdistinctsname,temp.sno,temp.ag
fromstudent,(selectsno,avg(grade)ag
fromsc
groupbysno)temp
wheretemp.ag>
andstudent.sno=temp.sno
orderbytemp.agdesc;
wheretemp.aglike'
6%'
方法3
selectsname,student.sno,avg(grade)
fromstudent,sc
wherestudent.sno=sc.sno
groupbystudent.sno,sname
havingavg(grade)>
orderbyavg(grade)desc;
例7、查询李大鹏同学所学课程门数及平均成绩(该题为例8做准备)。
selectcount(cno),avg(grade)
andsname='
wheresnoin(selectsno
例8、查询平均成绩大于李大鹏同学平均成绩的所有学生的学号、姓名和平均成绩。
selectdistinctsc.sno,sname,temp.ag
fromstudent,sc,(selectsno,avg(grade)ag
andstudent.sno=sc.sno)
andstudent.sno=sc.sno
andsc.sno=temp.sno;
selectstudent.sno,sname,avg(grade)
andstudent.sno=sc.sno);
例9、查询选修课程门数和平均成绩都大于李大鹏同学所选课程门数和平均成绩的所有的学生姓名。
selectdistinctsc.sno,sname,temp.ag,temp.ct
fromstudent,sc,(selectsno,avg(grade)ag,count(cno)ct
andtemp.ct>
(selectcount(cno)
selectdistinctstudent.sno,sname,avg(grade),count(sc.sno)
andcount(sc.sno)>
(selectcount(sc.sno)
例10、查询数据结构课程的成绩大于全班平均成绩的学生姓名。
selectsname
fromstudent,sc,course
wheregrade>
fromsc,course
whereo=o
andcname='
ando=o
wherecnamein(selectcname
fromcourse
wherecname='
))
ando=o;
例11、查询数据结构课程的成绩大于全班平均成绩的学生姓名、本人的数据结构成绩及全班的平均成绩。
selectsname,grade,temp.ag
fromstudent,sc,course,(selectavg(grade)ag
)temp
groupbysname,grade,temp.ag;
fromstudent,sc,cou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 34实 三四 34
![提示](https://static.bdocx.com/images/bang_tan.gif)