实验五数据查询
- 格式:doc
- 大小:133.00 KB
- 文档页数:4
软件学院
实验报告
课程:数据库原理与实用技术实验
实验5 数据查询
1、完成教科书163页的如下习题:
[3.2]b:select aid from agents where percents>=all(select percents from agents)
[3.5]:select cid ,aid from customers c,agents a where aid not in(select x.aid from orders x where x.cid =c.cid and x.aid = a.aid) and cid not in(select x.aid from orders x where x.cid =c.cid and x.aid = a.aid)
[3.8]a:select cid,max(dollars)as maxspent from orders group by cid
[3.8]b:select cid,avg(dollars) from orders group by cid
[3.11]b:
[3.11]f:select distinct pid from orders where cid in(select cid from customers where city='Dallas'
[3.11]j:update products set price=1.1*price where city='Duluth'or city='Dallas'
[3.11]l:select aid,[percent] from agents where aid in(select aid from orders where cid in (select cid from customers where city='Duluth')) order by [percent] desc
2、在“学生管理数据库”中完成如下查询:
(1)列出软件2班女同学的名单
select* from学生表where性别='女'and班级= '软件班'
(2)列出2002年元旦前注册的学生名单
select姓名from学生表where入学日期<'2002/1/1 0:00:00'
(3)列出所有年龄在19岁以下,或者女同学的名单
select* from学生表where年龄<19 or性别='女'
(4)列出没有先修课的课程名
select课程名from课程表where先修课is null
(5)列出既不是“电子系”,也不是“会计系”的学生的学号、姓名和所在院系
select姓名,学号,所在院系from学生表where所在院系<>'电子系'and所在院系<>'会计系'
(6)查询选修“C801”课程的学生的最高分
select max(成绩)as最高成绩from成绩表where课程号='c801'
(7)统计男、女学生各自的人数和平均年龄
select性别,count(学号)as人数,avg(年龄)as平均年龄from学生表group by性别'
(8)列出选修了一门以上课程的学生学号,及其选修门数
select学号,count(课程号)as选修科数from成绩表group by学号having count(课程号)>1
(9)查询“沈香娜”同学所在班级的所有学生的学号和姓名
select姓名,学号from学生表where班级in(select班级from学生表where姓名='沈香娜')
(10)统计每一年龄选修课程的学生人数
select年龄,count(学号)as个数from学生表where学号in(select学号from成绩表)group by年龄
难题:
1)在基本表“成绩表”中检索平均成绩最高的学生学号
select top 1 学号,avg(成绩)as最高平均成绩from成绩表group by学号order by avg(成绩) desc
2)求表S中男同学的每一年龄组(超过2人)有多少人?要求查询结果按人数升序排列,
人数相同按年龄降序排列。
select年龄,count(学号)as人数from学生表where性别='男' group by年龄having count(学号)>2 order by人数asc,年龄desc
3)列出选修了全部课程的学生学号和姓名
select姓名,学号from学生表where学号in(select学号from成绩表,课程表where成绩表.课程号=课程表.课程号group by学号having count(成绩表.课程号)=(select count(课程表.课程号)from课程表))
4)查询这样的学生,该生至少选修了学生20026001所选修的全部课程
select学号from成绩表where课程号in (select课程号from成绩表where学号='20026001')。