文档库 最新最全的文档下载
当前位置:文档库 › 数据库第六章答案

数据库第六章答案

select * from s
select * from c
select * from sc

1 查询所有汽车系学生的信息
select * from s where 系='汽车系'

2 查询里老师所教的课程号,课程名
select 课程号,课程名 from c where 教师 like '李%'

3 查询年龄大于20岁的女同学的学号和姓名
select 学号,姓名 from s where year(getdate()-year(出生日期))>20

4 查询学号为“J0401”所选修的全部课程成绩
select 成绩 from sc where 学号='J0401'
5 查询平均成绩都在80分以上的学生的学号及平均成绩
select * from sc
select 学号,avg(成绩) 平均成绩 from sc group by 学号 having avg(成绩)>80

6 查询至少有4人选修的课程号
select 课程号 至少有4人选修 from sc group by 课程号 having count(学号)>=4

7 查询c02号课程得最高分的学生学号(可用单表查询,嵌套查询)
select top 1 学号 from sc where 课程号='c02' order by 成绩 desc

select 学号 from sc where 成绩 = (select max(成绩) from sc where 课程号 = 'c02')

8 取出学号为“J0401”的学生选修的课程号和课程名

select 课程号,课程名 from c where 课程号 in (select 课程号 from sc where 学号 = 'J0401')

9 “李丽”所选修的全部课程名称

Select 课程名 from s,sc,c where s.学号= sc.学号 and sc.课程号=c.课程号 and 姓名='李丽'

10 所有成绩都在70分以上的学生姓名及所在系(可用嵌套查询)

select 姓名,系 as 所在系 from s where 学号 not in (select s.学号 from sc right join s on s.学号=sc.学号 where 成绩<70 or 成绩 is null)
select 姓名,系 as 所在系 from s where 学号 in (select 学号 from sc group by 学号 having min(成绩)>=70)

11 数据库成绩比C语言成绩好的学生(自身连接)

select * from s where 学号 in (select sc1.学号 from sc as sc1,sc as sc2 where sc1.学号=sc2.学号 and sc1.成绩>sc2.成绩 and sc1.课程号=(select 课程号 from c where 课程名='数据库')and sc2.课程号 =(select 课程号 from c where 课程名='C语言'))

12 至少选修了两门课及以上的学生姓名和性别(可用嵌套查询)
select 姓名,性别 from s where 学号 in(select 学号 from sc group by 学号 having count(*)!<2)

13 选修了李老师所讲课程的学生人数
select count(*) 学生人数 from sc where 课程号 =(select 课程号 from C where 教师 like '陈%')

14 “数据库”课程得最高分的学生的姓名、性别和所在系(可用嵌套查询)
select 姓名,性别,系 from s where 学号=(select top 1 学号 from sc where 课程号=(select 课程号 from c where 课程名='数据库') order by 成绩 desc)

15 显示所有课程的选修情况
select*from sc right join c on sc.课程号=c.课程号

16 取出没有选修“数据库”课程的学生姓名和年龄。

select 姓名,year(getdate())-year(出生日期)as 年龄 from s where 学号 not in (select 学号 from sc where 课

程号 in (select 课程号 from c where 课程名 = '数据库') )


17 没有选修李老师所讲课程的学生。

select * from s where 学号 not in (select 学号 from sc where 课程号=(select 课程号 from c where 教师 like '李%'))

18 取出选修了全部课程的学生和性别。

select 姓名, 性别 from s where 学号 = (select 学号 from sc group by 学号

having count (课程号)=(select count(*) from c))

19 检索至少选修课程“数据结构”和“C语言”的学生学号。

select 学号 from sc where 课程号 = (select 课程号 from c where 课程名='c语言')and 学号 in (select 学号 from c where 课程号=(select 课程号 from sc where 课程名='数据结构'))

20 检索学习课程号为C02的学生学号、姓名和所在系

select 姓名,学号 from s where 学号 in (select 学号 from sc where 课程号 ='c02')

21 检索选修课程号C01或C02的学生学号、姓名和所在系
select 学号,姓名,系 from s where 学号 in ( select 学号 from sc where 课程号 ='c01' or 课程号 ='c01' )

22 检索至少选修课程号为C01和C03的学生姓名
select 姓名 from s where 学号 in (select x.学号 from sc as x ,sc as y where x.学号=y.学号 and x.课程号 ='c01' and y.课程号 ='c03')

23 检索每个学生的年龄
select 学号,姓名,year(getdate())-year(出生日期)as 年龄 from s

24 在学生基本信息表S中检索学生的姓名和出生年份,输出的列名为STUDENT_NAME和BIRTH_YEAR
select 姓名 as STUDENT_NAME, year(出生日期) as BIRTH_YEAR from s
25
insert into sc values ('S0404', 'C06' ,'90')

26
delete from sc where 课程号 in (select 课程号 from c where 课程号='网络技术')

27
update sc set 成绩=成绩*1.1 where 学号 in (select 学号 from s where 性别='女')

28
select s.姓名 , count(*) 选修门数 from s,sc where s.学号=sc.学号 group by s.学号,s.姓名 having count(*) >3

29
select 课程号,count(*) 人数 from sc group by 课程号

30
select 学号,成绩 from sc where 课程号='c01' order by 成绩 desc
31
select 学号,avg(成绩) 平均成绩 from sc group by 学号
32
select * from s,c

33
select s.学号,s.姓名,avg(成绩) 平均成绩 from s,sc where s.学号=sc.学号 group by s.学号,s.姓名

34
select 学号,课程号 from sc

35
update sc set 成绩=成绩+10 where 学号='J0403' and 成绩<90
36
update sc set 成绩=成绩+10 where 成绩=( select min (成绩) from sc)
37
update sc set 成绩=成绩-10 where 成绩 in ( select top 3 成绩 from sc order by 成绩 desc)

38
update sc set 成绩=成绩-5 where 成绩 in ( select top 10 percent 成绩 from sc order by 成绩 desc)

39
select 课程号 from sc where 课程号 in (select c.课程号 from c,s where 性别='男' group by c.课程号 having count(c.课程号)>=2)
40
select * from s where 姓名 not like '王%'
41
select 姓名 from s where 性别=(select 性别

from s where 姓名= '李丽') and 系= (select 系 from s where 姓名= '李丽')
42
select count(distinct 课程号) as 课程门数 from sc

43
select avg(year(getdate())-year(出生日期)) as 平均年龄 from s where 学号 in( select 学号 from sc where 课程号='C04')

44
select 课程号, avg(成绩) 平均成绩 from sc where 课程号 in (select 课程号 from c where 教师 like '李%') group by 课程号

45
select 课程号, count(*) 选修人数 from sc group by 课程号 having count(*)>=3 order by 课程号 asc ,count(*) desc

相关文档