实验四复杂查询.docx
- 文档编号:24304693
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:13
- 大小:327.49KB
实验四复杂查询.docx
《实验四复杂查询.docx》由会员分享,可在线阅读,更多相关《实验四复杂查询.docx(13页珍藏版)》请在冰豆网上搜索。
实验四复杂查询
实验四复杂查询
一、实验目的
掌握两个表以上的连接查询的应用,包括嵌套查询。
二、实验内容
(1)查询比“林红”年纪大的男学生信息。
select*fromStudent
whereSex='男'and
YEAR(Birth)-(selectYEAR(Birth)
fromStudentwhereSname='林红')<0
(2)检索所有学生的选课信息,包括学号、姓名、课号、课程名、成绩。
selectSC.Sno,Sname,Sex,Classno,Cname,Grade
fromStudents,SC,Coursec
wheres.Sno=SC.SnoandSC.cno=o
(3)查询已选课学生的学号、姓名、课程名、成绩。
selectSC.Sno,Sname,Cname,Grade
fromStudents,coursec,SC
wheres.sno=SC.snoando=SC.cno
(4)查询选修了“C语言程序设计”的学生的学号和姓名。
selectsc.Sno,Sname
fromStudents,coursec,sc
wherec.Cname='C语言程序设计'ands.Sno=sc.Snoandsc.Cno=c.Cno
(5)查询与“张虹”在同一个班级的学生学号、姓名、家庭住址。
a.用子查询
selectSno,Sname,Home_addr
fromStudent
whereClassno='051'andSname!
='张虹'
b.用连接查询
selectSno,Sname,Home_addr
fromStudent
whereClassno=(selectClassnofromStudentwhereSname='张虹')
andSname!
='张虹'
(6)查询其他班级中比“051”班所有学生年龄大的学生的学号、姓名。
selectSno,Sname
fromStudent
whereClassno<>'051'
andBirth whereClassno='051') (7)(选作)查询选修了全部课程的学生姓名。 本题使用除运算的方法。 由题意可得另一种语言,没有一个选了课的学生没有选course表里的课程。 那么,我们需要两个NOTEXISTS表示双重否定;另一种思路可详见书例4.52 selectSnamefromStudent wherenotexists( select*fromCourse wherenotexists( select*fromSC whereSno=Student.sno andcno=Co)) (8)(选作)查询至少选修了学生“20110002”选修的全部课程的学生的学号,姓名。 selectSno,SnamefromStudent whereSnoin( selectdistinctSno fromSCasSC1 wherenotexists( select*fromSCasSC2 whereSC2.Sno='20110002' andnotexists( select*fromSCasSC3 whereSC3.Sno=SC1.Snoand SCo=SCo)) ) (9)检索学生的学号、姓名、学习课程名及课程成绩。 selects.Sno,Sname,Cname,Grade fromStudents,Coursec,SC wheres.Sno=sc.Snoandsc.Cno=c.Cno (10)检索选修了“高数”课且成绩至少高于选修课程号为“002”课程的学生的学号、课程号、成绩,并按成绩从高到低次序排列。 由题意得,选修了高数课的学生的成绩要高于选修002课号课程的学生的成绩 selectdistinctSno,Cno,Grade fromSC whereCnoin( selectCnofromCourse whereCname='高数') andGrade>(selectMAX(Grade) fromSCwherecno='002') orderbyGradedesc (11)检索选修3门以上课程的学生的学号、总成绩(不统计不及格的课程),并要求按总成绩的降序排列出来。 selectSno,sum(grade)as总成绩 fromSC whereSnoin( selectSnofromSC groupbySno havingcount(*)>3) andGrade>=60 groupbySno orderby总成绩desc (12)检索多于3名学生选修的并以3结尾的课程号的平均成绩。 selectavg(Grade)as平均成绩 fromSC whereCnolike'%3' groupbyCno havingcount(Cno)>3 (13)检索最高分与最低分之差大于5分的学生的学号、姓名、最高分、最底分。 selectdistinctSC.Sno学号,Sname姓名, max(grade)as最高分,min(grade)as最低分 fromStudent,SC whereSC.Sno=Student.Sno groupbySC.Sno,Sname havingmax(grade)-min(grade)>5 (14)外连接 对实验二中的表6和表7做一个外连接查询,显示每门课程的课号、课名、选修该门课的学号、成绩,没有同学选修的课程(如Visual_Basic)也要在查询结果中。 selectc.Cno课号,Cname课名,Sno学号,Grade成绩 fromCoursecleftouterjoinSC on(c.Cno=SC.Cno) (15)创建一个表Student_other,结构同Student,输入若干记录,部分记录和Student表中的相同。 创建过程: createtableStudent_other( Snochar(8)primarykey, Snamevarchar(8)notnull, Sexchar (2)notnull, Birthsmalldatetimenotnull, Classnochar(3)notnull, Entrance_datesmalldatetimenotnull, Home_addrvarchar(40), Sdeptchar (2)notnull, Postcodechar(6) ) 随意输入几条Student表中没有的信息,完成创建 a.查询同时出现在Student表和Student_other表中的记录 select*fromstudent_otherso,Students whereso.Sno=s.Sno b.查询Student表和Student_other表中的全部记录 select*fromstudent union select*fromstudent_other (16)(选作)创建一个数据库Student_info_other,参数自定。 创建过程: 新建数据库 名称确定,参数自定义,然后“确定”即可 a.当前数据库为Student_info,将Student_info数据库中的Student_other复制到Student_info_other中。 select*intoStudent_info_other.dbo.Student_other fromStudent_info.dbo.Student_other b.查询同时出现在Student表和Student_info_other数据库Student_other表中的记录。 select*fromStudent_info_other.dbo.student_otherso, Student_info.dbo.Students whereso.sno=s.sno
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 复杂 查询