实验答案(四-五-六)参考答案_第1页
实验答案(四-五-六)参考答案_第2页
实验答案(四-五-六)参考答案_第3页
实验答案(四-五-六)参考答案_第4页
实验答案(四-五-六)参考答案_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、实验四 简单查询和连接查询1. 简单查询实验用 Transact-SQL 语句表示下列操作,在“学生选课“数据库中实现其数据查询操作:(1) 查询数学系学生的学号和姓名。select sno,sname from student where dept= 数学系 ;(2) 查询选修了课程的学生学号。select distinct(sno) from sc;(3) 查询选修课程号为 0101 的学生学号和成绩,并要求对查询结果按成绩降序排列, 如果成绩相同则按学号升序排列。select distinct(sno),grade from sc where cno=0101 order by grad

2、e desc,sno asc;(4) 查询选修课程号为 0101 的成绩在 80-90 分之间的学生学号和成绩, 并将成绩乘以 系数 0.8 输出。select distinct(sno),grade*0.8 as sore from sc where cno=0101 and grade between 80 and 90;(5) 查询数学系或计算机系姓张的学生的信息。select * from student where dept in (数学系 , 计算机系 )and sname like 张 % ;(6) 查询缺少了成绩的学生的学号和课程号。select sno,cno from sc

3、 where grade is null;2. 连接查询实验用 Transact-SQL 语句表示,并在“学生选课”数据库中实现下列数据连接查询操作:(1) 查询每个学生的情况以及他(她)所选修的课程。select student.*,ame from student,sc,course where student.sno=sc.sno and o=o;(2) 查询学生的学号、姓名、选修的课程名及成绩。select student.sno,sname,cname,grade from student,sc,course where student.

4、sno=sc.sno and o=o;(3) 查询选修离散数学 课程且成绩为 90 分以上的学生学号、姓名及成绩。select student.sno,sname,gradefrom student,sc,course where student.sno=sc.sno and o=o and cname= 离散数学 and grade=90;(4) 查询每一门课的间接先行课(即先行课的先行课) 。select o,second.pcno from course as first,course as second where

5、 first.pcno=o;实验五 嵌套查询用 TransacTransact-SQL 语句表示,在学生选课库中实现其数据嵌套查询操作:(l) 查询选修了离散数学的学生学号和姓名。select sno,snamefrom studentwhere sno in(select snofrom scwhere cno=(select cnofrom coursewhere cname= 离散数学 );(2) 查询 0101 课程的成绩高于张林的学生学号和成绩。select sno,grade from sc where cno=0101 and grade (select gra

6、de from sc where cno=0101 and sno=(select snoFrom studentWhere sname= 张林 );(3) 查询其他系中年龄小于计算机系年龄最大者的学生。select *from studentwhere dept 计算机系 and age(select max(age)from studentwhere dept= 计算机系 );(4) 查询其他系中比计算机系学生年龄都小的学生。(3) 中的 max 换成 min 即可。(5) 查询同牟万里数据库原理课程分数相同的学生的学号。select snofrom scwhere grade=(sele

7、ct gradefrom student,sc,coursewhere student.sno=sc.sno and o=o and ame= 数据库原理 and sname= 牟万里 );(6) 查询选修了 0206 课程的学生姓名。select snamefrom studentwhere sno in (select snofrom scwhere cno=0206);(7) 查询没有选修 0206 课程的学生姓名。 在 (5) 的 in 前加 not 即可。(8) 查询选修了全部课程的学生的姓名。SELECT SNAMEFROM STUDE

8、NTWHERE SNO IN (SELECT SNOFROM SCGROUP BY SNOHAVING COUNT(*)=( SELECT COUNT(*)FROM COURSE);select snamefrom studentwhere not exists (select *from coursewhere not exists (select *from scandwhere sno=student.sno cno=o);(9) 查询与学号为“ 09001103”的学生所选修的全部课程相同的学生学号和姓名。 select sno,snameFrom studentWh

9、ere sno09001103 and not exists(select *From sc as xWhere sno=09001103 and not exists精选资料,欢迎下载(10)(select * From sc as yWhere y.sno=student.sno and o=o ) ;查询至少选修了学号为“ 09001103”的学生所选修的全部课程的学生学号和姓名。select sno,snamefrom studentwhere sno in(select scx.snofrom sc scxwhere not exists(select *from s

10、c scywhere scy.sno=09001103 and not exists(select *from sc sczwhere scz.sno=scx.sno and o=o);实验六 组合查询和统计查询在学生选课数据库中实现其查询操作:成绩。(1) 查找选修 “计算机基础” 课程的学生成绩比此课程的平均成绩大的学生学号, select x.sno,x.gradeFrom sc as xWhere x.grade(select avg(y.grade)From sc as y,course as cWhere ame=计算机基础 ) and o=

11、(select cno From courseWhere cname= 计算机基础 );(2) 查询选修计算机基础课程的学生的平均成绩。 select avg(grade)From scWhere sno in(select snoFrom scWhere cno=(select cnoFrom courseWhere cname= 计算机基础 );(3) 查询年龄大于女同学平均年龄的男同学姓名和年龄。select sname,age精选资料,欢迎下载From studentWhere sex=男 and age(select avg(age)From studentWhere sex=女 )

12、;(4) 列出各系学生的总人数,并按人数进行降序排列。 select dept ,count(*) as totalFrom studentGroup by dept order by total desc;(5) 统计各系各门课程的平均成绩。 select dept ,cno,avg(grade)From student,scGroup by dept,cno(6) 查询选修计算机基础和离散数学的学生学号和平均成绩。select s1.sno,avg(grade) as 平均分From sc as s1Where 计算机基础 in(select cnameFrom courseWhere cno in(

温馨提示

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

评论

0/150

提交评论