码迷,mamicode.com
首页 > 数据库 > 详细

数据库5.27习题

时间:2015-05-31 10:41:26      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

2015.5.27

数据库SQLServer练习题题目 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。

表1-1数据库的表结构

表(一)Student (学生表)

属性名 数据类型 可否为空 含 义

Sno Char(3) 否 学号

Sname Char(8) 否 学生姓名

Ssex Char(2) 否 学生性别

Sbirthday datetime 可 学生出生年月

Class Char(5) 可 学生所在班级

表(二)Course(课程表)

属性名 数据类型 可否为空 含 义

Cno Char(5) 否 课程号

Cname Varchar(10) 否 课程名称

Tno Char(3) 否 教工编号

表(三)Score(成绩表)

属性名 数据类型 可否为空 含 义

Sno Char(3) 否 学号

Cno Char(5) 否 课程号

Degree Decimal(4,1) 可 成绩

主码:Sno+ Cno

表(四)Teacher(教师表)

属性名 数据类型 可否为空 含 义

Tno Char(3) 否 教工编号

Tname Char(4) 否 教工姓名

Tsex Char(2) 否 教工性别

Tbirthday datetime 可 教工出生年月

Prof Char(6) 可 职称

Depart Varchar(10) 否 教工所在部门

表1-2数据库中的数据

表(一)Student

Sno Sname Ssex Sbirthday class

108 曾华 男 1977-09-01 95033

105 匡明 男 1975-10-02 95031

107 王丽 女 1976-01-23 95033

101 李军 男 1976-02-20 95033

109 王芳 女 1975-02-10 95031

103 陆君 男 1974-06-03 95031

 表(二)Course

Cno Cname Tno

3-105 计算机导论 825

3-245 操作系统 804

6-166 数字电路 856

9-888 高等数学 831

表(三)Score

Sno Cno Degree

103 3-245 86

105 3-245 75

109 3-245 68

103 3-105 92

105 3-105 88

109 3-105 76

101 3-105 64

107 3-105 91

108 3-105 78

101 6-166 85

107 6-166 79

108 6-166 81

表(四)Teacher

Tno Tname Tsex Tbirthday Prof Depart

804 李诚 男 1958-12-02 副教授 计算机系

856 张旭 男 1969-03-12 讲师 电子工程系

825 王萍 女 1972-05-05 助教 计算机系

831 刘冰 女 1977-08-14 助教 电子工程系

1、查询Student表中的所有记录的Sname、Ssex和Class列。

2、查询教师所有的单位即不重复的Depart列。

3、查询Student表的所有记录。

4、查询Score表中成绩在60到80之间的所有记录。

5、查询Score表中成绩为85、86或88的记录。

6、查询Student表中95031班或性别为“女”的同学记录。

7、以Class降序查询Student表的所有记录

8、以Cno升序、Degree降序查询Score表的所有记录。

9、查询95031班的学生人数。

10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

11、查询每门课的平均成绩。

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

13、查询分数大于70、小于90的Sno列。

14、查询所有学生的Sname、Cno和Degree列。

15、查询所有学生的Sno、Cname和Degree列。

16、查询所有学生的Sname、Cname和Degree列。

17、查询95033班学生的平均分。

18、

假设使用如下命令建立了一个grade表:

create table grade(low int(3),upp int(3),rank char(1))

insert into grade values(90,100,’A’)

insert into grade values(80,89,’B’)

insert into grade values(70,79,’C’)

insert into grade values(60,69,’D’)

insert into grade values(0,59,’E’)

现查询所有同学的Sno、Cno和rank列。

19、查询选修3-105课程的成绩高于109号同学成绩的所有同学的记录。

20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。

21、查询成绩高于学号为109、课程号为3-105的成绩的所有记录。

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

23、查询张旭教师任课的学生成绩。

24、查询选修某课程的同学人数多于5人的教师姓名。

25、查询95033班和95031班全体学生的记录。

26、查询存在有85分以上成绩的课程Cno。

27、查询出计算机系教师所教课程的成绩表。

28、查询“计算机系”与“电子工程系”不同职称的教师的Tname和Prof。

29、查询选修编号为3-105课程且成绩至少高于选修编号为3-245的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

30、查询选修编号为3-105且成绩高于选修编号为3-245课程的同学的Cno、Sno和Degree。

31、查询所有教师和同学的name、sex和birthday。

32、查询所有“女”教师和“女”同学的name、sex和birthday。

33、查询成绩比该课程平均成绩低的同学的成绩表。

34、查询所有任课教师的Tname和Depart。

35、查询所有未讲课的教师的Tname和Depart。

36、查询至少有2名男生的班号。

37、查询Student表中不姓“王”的同学记录。

38、查询Student表中每个学生的姓名和年龄。

39、查询Student表中最大和最小的Sbirthday日期值。

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

41、查询“男”教师及其所上的课程。

42、查询最高分同学的Sno、Cno和Degree列。

43、查询和“李军”同性别的所有同学的Sname。

44、查询和“李军”同性别并同班的同学Sname。

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

部分题解题步骤

12!
select * from score
select avg degree from score
select avg degree from score where cno like‘3%‘
select avg degree from score where cno like‘3%‘ group by cno having count(*)>=5
17!
select * from student
select * from score
select sno from student where class=‘95033‘
select avg from score where sno in(‘101‘,‘107‘,‘108‘)
18!
select * from grade
select * from score
select * from score join rank on score.degree>grade.low and score.degree<grade.upp
20!
select sno,count(*) from score group by sno having count(*)>1
select * from score where sno in (‘101‘,‘103‘,‘105‘,‘107‘,‘108‘,‘109‘) //查出选多门课程学生数
select degree from score---92
select sno from student---103
select sno,count(*) from score where sno!=‘103‘ group by sno having count(*)>1//查出非最高分成绩记录

24!
select * from teacher
select * from score
select * from course
select cno from score group by cno having count(*)>=5
select tno from course where cno in (cno=‘3-105‘)
select tname from teacher where tno in(tno=‘825‘)
select tname from teacher where tno in(select tno from course where cno in (select tno from course where cno in (cno=‘3-105‘)))

29!选修3-105或3-245
select * from score
select max.degree from score where cno=‘3-245‘---86
select * from score where cno=‘3-105‘ and score.degree>86 order by degree desc
select * from score where cno=‘3-105‘ and score.degree>(select max.degree from score where cno=‘3-245‘) order by degree desc

30!选修3-105且3-245(运用相关子查询)
select * from score
select * from score where cno in(‘3-105‘,‘3-245‘)
select sno from score where cno in(‘3-105‘,‘3-245‘) group by sno having count(*)>=2
select * from score s1 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-105‘
select * from score s2 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-245‘
select sno from score s1 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-105‘ and exists(select * from score s2 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-245‘ and s1.sno=s2.sno and s1.degree>s2.degree)
select sno from score where sno in(‘103‘,‘109‘)
select sno from score where sno in(select sno from score s1 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-105‘ and exists(select * from score s2 where (sno in(‘103‘,‘105‘,‘109‘)) and cno=‘3-245‘ and s1.sno=s2.sno and s1.degree>s2.degree))
1.找出既选3-105又选3-245的同学的学号---使用分组查询
2.找出3-105成绩大于3-245成绩学生的学号---使用相关子查询(3-105的成绩,3-245的成绩)
3.根据第2步找出的学生学号找出他的所有成绩

33!运用相关子查询---从第1个表查询的结果跟第2个表的相关列进行对比
select * from score s1
select * from score s2 group by cno,avg.degree
select * from score s1 where exists(select * from score s2 group by cno,avg.degree having s1.cno=s2.cno and s1.degree<avg)

34!
法一:相关子查询
select * from teacher
select * from course
select * from teacher where exists(select * from course where teacher.tno=course.tno)
法二:无关子查询
select * from course
select * from teacher
select distinct tno from course
select * from teacher where tno in(select distinct tno from course)
法三:连接查询
select * from course join teacher on teacher.tno=course.tno

40!
select * from student order by class desc,sbirthday asc

数据库5.27习题

标签:

原文地址:http://www.cnblogs.com/kxy3-1314/p/4541493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!