数据库作业答案

2020-03-27 20:08

实验六

1、查询所有数学系学生的信息 。 --select * from s where 系='数学系' 2、查询李老师所教的课程号、课程名

--select 课程号,课程名 from c where 教师 like '李%' 3、查询年龄大于20岁的女同学的学号和姓名。

--select 学号,姓名 from s where year(getdate())-year(出生日期)>20 and 性别='女'

4、查询学号为‘H0301’所选修的全部课程成绩。 --select 成绩 from sc where 学号= 'H0301'

5、查询平均成绩都在80分以上的学生学号及平均成绩。

--select 学号,AVG(成绩) from sc group by 学号 having AVG(成绩)>80

6、查询至少有6人选修的课程号。

--select 课程号 from sc group by 课程号 having count(*)>=6 7、查询C02号课程得最高分的学生的学号

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

8、查询学号为’J0101’的学生选修的课程号和课程名

--select 课程号,课程名 from c,sc where 学号 ='j0101' and c.课程号=sc.课程号

9、‘李小波’所选修的全部课程名称。

--select c.课程名 from s,c,sc where s.学号=sc.学号 and c.课程号=sc.课程号 and 姓名='李小波'

10、所有成绩都在70分以上的学生姓名及所在系。

--select 姓名,系 from s,sc where s.学号=sc.学号 group by 姓名, 系 having min(成绩)>=70

11、英语成绩比数学成绩好的学生 select sc2.学号

from c c1,c c2,sc sc1,sc sc2

where c1.课程名='英语' and c2.课程名='数学' and sc1.成绩>sc2.成绩

and sc1.学号=sc2.学号 and c1.课程号=sc1.课程号 and c2.课程号=sc2.课程号

或:

Select sc1.学号 from sc sc1, sc sc2 where sc1.学号=sc2.学号 And (sc1.课程号 in (select 课程号 from c where 课程名= ‘英语’))

And (sc2.课程号 in (select 课程号 from c where 课程名= ‘数学’))

And sc1.成绩>sc2.成绩

12、至少选修了两门课及以上的学生的姓名和性别 select 姓名,性别 from s,sc

where s.学号=sc.学号 group by 姓名, 性别 having count(*)>=2 13、选修了李老师所讲课程的学生人数 select count(*) from c,sc

where 教师 like '李%' and c.课程号=sc.课程号 group by sc.课程号

14、‘操作系统’课程得最高分的学生的姓名、性别、所在系 select 姓名,性别,系 from s,sc,c

where s.学号=sc.学号and c.课程号=sc.课程号 and c.课程名=’操作系统’ and 成绩=100

15、显示所有课程的选修情况。

select * from c left join sc on c.课程号=sc.课程号

16、取出没有选修‘操作系统’课程的学生姓名和年龄 select distinct(姓名),(year(getdate())-year(出生日期))as 年龄 from s,c,sc

where sc.学号=s.学号 and c.课程号=sc.课程号 and c.课程号 not in (select 课程号 from c where 课程名='操作系统')

(没选课的学生呢?)

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

(select 学号from sc,c where sc.课程号=c.课程号 and 课程名='操作系统')

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

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

select 姓名 from s where 姓名 not in

(select 姓名 from s left join sc on s.学号=sc.学号

where sc.课程号 in (select c.课程号 from c where 教师 like '李%') )

(没选课的学生呢?)

select 学号, 姓名 from s where 学号 not in ( select 学号 from sc where (课程号 in

(select 课程号 from c where 教师 like '李%')))

18、取出选修了全部课程的学生姓名,性别。 Select s.姓名,s.性别 from s where not exists (select * from c where not exists

(select * from sc where sc.学号=s.学号 and sc.课程号 = c.课程号))

19、检索至少选修课程“数据结构”和“C语言”的学生学号。 select sc.学号, c.课程名 from c,sc where c.课程名 = '数据结构' and c.课程号 = sc.课程号 and 学号 in

( select sc.学号 from sc ,c where c.课程名 = 'C语言' and c.课程号 = sc.课程号)

20、检索学习课程号为C02的学生学号与姓名。

--select s.学号,姓名 from sc,s where 课程号='c02' and s.学号=sc.学号

21、检索选修课程号为C01或C02的学生学号,姓名和所在系 select distinct s.学号, s.姓名,s.系from s inner join sc on s.学号= sc.学号where sc.课程号='C02' or sc.课程号= 'C01'

或:

select 学号, 姓名, 系from s

where 学号in (select 学号from sc where 课程号='C02' or 课程号='C01') --课程号in ('C02','C01') ) 22、检索至少选修课程号为C01和C03的学生姓名。 --select s.姓名

from sc,s where sc.学号=s.学号 and 课程号='c01' and s.学号 in (select 学号 from sc where 课程号='c03') 或:

select 姓名from s

where 学号in (select 学号from sc where 课程号='C01') and 学号in (select 学号from sc where 课程号='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、向学生选课数据表SC中插入一个元组(S0404,C06,90) insert into sc values ('S0403','C06',90)

26、把课程名为VB的成绩从学生选课数据表SC中删除

delete from sc where 课程号 IN (SELECT 课程号 FROM C

WHERE 课程名='VB')

27、把女同学的成绩提高10%。

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

28、列出选修课程超过3门的学生姓名及选修门数。

select S.姓名,count(*) as 选修门数 from S,SC where S.学号 =SC.学号 group by S.姓名 having count(*)>3

29、求选修了各课程的学生的人数。

select 课程号,count(*) as 人数 from SC group by 课程号

30、在学生选课数据表SC中,求选修课程C01的学生的学号和得分,并将结果按分数降序排序。

select 学号,成绩 from sc where 课程号='c01' order by 成绩 desc 31、查找每个同学的学号及选修课程的平均成绩情况。 select 学号, avg(成绩) as 平均成绩 from sc group by 学号 32、列出学生所有可能的选课情况。 --select * from c cross JOIN s

33、列出每个同学的学号、姓名及选修课程的平均成绩情况,没有选修的同学也列出。

select s.学号, s.姓名, avg(成绩) from sc right join s on sc.学号=s.学号 group by s.学号, s.姓名

34、列出每个同学的学号及选修课程号,没有选修的同学也列出 select s.学号,课程号 from sc right join s on s.学号 = sc.学号 35、如果学号为J0404的学生的成绩少于90,则加上10分。 update sc set 成绩 = 成绩 + 10 where 学号 = 'J0404' and 成绩 < 90

36、将成绩最低的学生的成绩加上10分。

--update sc set 成绩=成绩+10 where sc.成绩=(select min(成绩)from sc)

37、将前3名成绩最高的学生的成绩减去10分。

--update sc set 成绩=成绩 - 10 where 成绩 in (select top 3 成绩 from sc order by 成绩 DESC)

update sc set sc.成绩= sc.成绩-10

from (select top 3 * from sc order by 成绩desc) as t where t.成绩=sc.成绩

38、将前10%成绩最低的学生的成绩减去5分。 update SC set 成绩 = 成绩 - 5 where 成绩 in (select top 10 percent 成绩 from SC order by 成绩)

39、检索至少有两名男生选修的课程名。

--select 课程名 from s,c,sc where 性别='男' and s.学号=sc.学号 and sc.课程号=c.课程号 group by 课程名 having count(*) >=2

40、检索学生基本信息表S中不姓“王”的学生记录。 --select * from s where 姓名 not like '王%'

41、检索和“李军”同性别并同系的同学的姓名。 select 系,姓名,性别 from s where 性别 in (select 性别 from s where 姓名 = '李军')

and 系 in (select 系 from s where 姓名 = '李军') 42、统计被学生选修的课程门数。

--select count(distinct 课程号) 课程门数 from sc 43、求选修C04课程的学生的平均年龄。

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

44、求刘老师所授课程的每门课程的学生平均成绩。 --select sc.课程号,avg(成绩) from sc,c

where 教师 like '刘%' and sc.课程号 = c.课程号 group by sc.课程号

或:select sc.课程号, c.教师, avg(成绩) as 平均成绩from c, sc where sc.课程号in (select c.课程号from c where 教师like '刘%') and sc.课程号=c.课程号

group by sc.课程号, c.教师

45、统计每门课程的学生选修人数(超过10人的课程才统计)。 ----要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。

--select 课程号,count(*) as 总人数 from sc --group by 课程号 --having count(*)>10

--order by总人数 desc, 课程号 视图作业:

1. create view stud_view as

select 姓名, 课程名, 成绩 from s, c, sc

where s.学号=sc.学号 and c.课程号=sc.课程号 3. create view teacher_view as

select 教师, c.课程号, 课程名, count(sc.课程号) as 选修人数 from s, sc

where c.课程号=sc.课程号


数据库作业答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:煤矿安全管理人员安全培训题库

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: