最新学生选课数据库SQL语句练习题(详细分解答案)

发布时间:2019-12-15 21:55:30   来源:文档文库   
字号:

一、设有一数据库,包括四个表:学生表(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表中的所有记录的SnameSsexClass列。

select sname,ssex,class from student;

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

select distinct depart from Teacher;

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

select * from student;

-- 4、查询Score表中成绩在6080之间的所有记录。

select * from score where degree between 60 and 80;

附件(二):调查问卷设计

3)个性体现-- 5、查询Score表中成绩为858688的记录。

select * from score where degree in(85,86,88);

自制性手工艺品。自制饰品其实很简单,工艺一点也不复杂。近两年来,由于手机的普及,自制的手机挂坠特别受欢迎。-- 6、查询Student表中“95031”班或性别为的同学记录。

合计 50 100% select * from student where class = '95031' or ssex='';

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

select * from student order by class desc;

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

select * from score order by cno,degree desc;

beadorks公司成功地创造了这样一种气氛:商店和顾客不再是单纯的买卖关系,营业员只是起着参谋的作用,顾客成为商品或者说是作品的作参与者,营业员和顾客互相交流切磋,成为一个共同的创作体

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

select class,count(*) as 学生人数 from student

我们大学生没有固定的经济来源,但我们也不乏缺少潮流时尚的理念,没有哪个女生是不喜欢琳琅满目的小饰品,珠光宝气、穿金戴银便是时尚的时代早已被推出轨道,简洁、个性化的饰品成为现代时尚女性的钟爱。因此饰品这一行总是吸引很多投资者的目光。然而我们女生更注重的是感性消费,我们的消费欲望往往建立在潮流、时尚和产品的新颖性上,所以要想在饰品行业有立足之地,又尚未具备雄厚的资金条件的话,就有必要与传统首饰区别开来,自制饰品就是近一两年来沿海城市最新流行的一种。 group by class having class='95031';

1DIY手工艺市场状况分析

我们熟练的掌握计算机应用,我们可以在网上搜索一些流行因素,还可以把自己小店里的商品拿到网上去卖,为我们小店提供了多种经营方式。-- 10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

select sno,cno,degree,

(select max(degree) from score) as maxscore--计算最高分

1-4大学生购买手工艺制品目的 from score where degree= (select max(degree) from score);

-- 11、查询‘3-105’号课程的平均分。

select avg(degree) as avgdegree

from score group by cno having cno='3-105';

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

select avg(degree) as avgdegree from score group by cno --按照课程分组取平均值

having cno= (select cno from score group by cno having count(*)>=5)--至少有5名学生选修的课程

and cno like '3%';--3开头的课程

-- 13、查询最低分大于70,最高分小于90Sno列。

select sno,max(degree)as maxdegree,min(degree) as mindegree from Score group by sno

having max(degree)<90 and min(degree)>70

-- 14、查询所有学生的SnameCnoDegree列。

select sname,cno,degree from student

join score on student.sno=score.sno;

-- 15、查询所有学生的SnoCnameDegree列。

select sno,cname,degree from Score

join course on Score.cno=course.cno;

-- 16、查询所有学生的SnameCnameDegree列。

select sname,cname,degree from student

join score on student.sno=score.sno

join course on Score.cno=course.cno;

-- 17、查询“95033”班所选课程的平均分。

select avg(degree) as avgdegree from score where sno in(select sno from student where class='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)

--现查询所有同学的SnoCnorank列。

select sno,cno,

(case when degree between 90 and 100 then 'A'

when degree between 80 and 89 then 'B'

when degree between 70 and 79 then 'C'

when degree between 60 and 69 then 'D'

when degree between 0 and 59 then 'E'

END) as rank from score;

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

select * from score where cno='3-105'and degree>(select degree from score where sno='109' and cno='3-105');

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

select * from score where sno in--选学多门课程的同学中分数为非最高分成绩的同学的全记录

(select sno from score group by sno having count(cno)>1--选学多门课程的同学

intersect--取交集为选学多门课程的同学中分数为非最高分成绩的同学。

select distinct sno from score where sno not in( --分数为非最高分成绩的同学

select sno from score where degree=(select max(degree) from score)))--分数最高成绩的同学

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

方法1select * from score where sno in--选学多门课程的同学中分数为非同课程最高分成绩的同学的全记录

(select sno from score group by sno having count(cno)>1--选学多门课程的同学

intersect--取交集为选学多门课程的同学中分数为非同课程最高分成绩的同学。

select distinct sno from score where sno not in(--非同课程分数最高成绩的同学

select distinct sno from score where degree in (--同课程分数最高成绩的同学

select max(degree)from score group by cno)))--同课程分数最高成绩

方法2select * from score where sno in--选学多门课程的同学中分数为非同课程最高分成绩的同学的全记录

(select sno from score group by sno having count(cno)>1-- 选学多门课程的同学

intersect -- 取交集为选学多门课程的同学中分数为同课程非最高分成绩的同学

select distinct sno from score where sno not in -- 选出非同课程最高分成绩的同学

(select distinct sno from score as s1

where degree=(select max(degree) from score as s2 where s1.cno=s2.cno group by cno)));-- 使用关联子查询选出同课程最高分成绩的同学

-- 22、查询1975年之后出生的学生的所学课程以及成绩。

select sname,Cname,degree from student

join score on student.sno=score.sno

join course on score.cno=course.cno

where sbirthday>='1975-01-01';

-- 23、查询和学号为107的同学同年出生的所有学生的SnoSnameSbirthday列。

select sno,sname,sbirthday from student where datepart(year,sbirthday)=

(select datepart(year,sbirthday) from student where sno='107')--学号为107的同学的出生年份

and sno not in('107');--排除学号为107的同学

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

select degree from score where cno=

(select cno from course

join teacher on teacher.tno=course.tno

where tname='张旭');--张旭老师所任课程

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

select tname from teacher

join course on teacher.tno=course.tno

where cno in (select cno from score

group by cno having count(*)>5);-- 多于5名同学选修的课程

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

select * from student where class in('95033','95031');

-- 27、查询存在有85分以上成绩的课程Cno.

select distinct cno from score where degree>85;

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

select score.cno,degree from score

join course on score.cno=course.cno

where tno in

(select tno from teacher where depart='计算机系');--计算机系教师的教师编号

-- 29、查询计算机系电子工程系不同职称的教师的TnameProf

select tname,prof from teacher where depart in('计算机系','电子工程系')--“计算机系电子工程系所有教师TnameProf

and prof not in --“计算机系电子工程系不同职称的教师Prof

(select prof from teacher where depart ='计算机系'

intersect

select prof from teacher where depart ='电子工程系')--“计算机系电子工程系相同职称的教师Prof

-- 30、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同学的CnoSnoDegree,并按Degree从高到低次序排序。

select cno,sno,degree from score where

cno='3-105'--选修编号为“3-105”课程的同学

and degree>any -- 大于任意一个选修编号为“3-245”的同学的成绩

(select degree from score where cno='3-245')--选修编号为“3-245”的同学的成绩

order by degree desc

-- 31、查询选修编号为“3-105“课程且成绩高于所有选修编号为“3-245”的同学的CnoSnoDegree,并按Degree从高到低次序排序。

select cno,sno,degree from score where

cno='3-105'--选修编号为“3-105”课程的同学

and degree>all -- 大于所有选修编号为“3-245”的同学的成绩

(select degree from score where cno='3-245')--选修编号为“3-245”的同学的成绩

order by degree desc

-- 32、查询所有教师和同学的namesexbirthday.

select sname as name,ssex as sex,sbirthday as birthday from student

union

select tname as name,tsex as sex,tbirthday as birthday from teacher

-- 33、查询所有教师和同学的namesexbirthday.

select sname as name,ssex as sex,sbirthday as birthday from student where ssex=''

union

select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex=''

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

select * from score as s1 where degree<

(select avg(degree) from score as s2 group by cno having s1.cno=s2.cno)

-- 35、查询所有任课教师的TnameDepart.

select tname,depart from teacher where tno in

(select tno from course)-- 课程表中存在的教师编号

-- 36 查询所有未讲课的教师的TnameDepart.

select tname,depart from teacher where tno in

(select tno from course where cno not in(select cno from course group by cno))

备注:如果课程表中课程确定不为空,也可以如下编写:

select Tname,Depart from Teacher where Tno not in

(select Tno from Course)

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

select class,ssex,count(ssex) as 男生人数 from student group by class,ssex having ssex='' and count(ssex)>1

select Class,COUNT(*) from Student where Ssex=''group by Class having COUNT(*)>=2

-- 38、查询Student表中不姓的同学记录。

select * from student where sname not like '%'

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

select sname,datediff(year,Sbirthday,current_timestamp)as 年龄 from student

select sname,datediff(year,Sbirthday,getdate()) as 年龄 from student

select sname,datepart(year,getdate())-datepart(year,Sbirthday) as 年龄 from student

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

select datepart(year,max(sbirthday))as max,datepart(year,min(sbirthday)) as min from student;

select max(year(sbirthday))as max,min(year(sbirthday)) as min from student;

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

select * from student order by class desc,Sbirthday

-- 42、查询教师及其所上的课程。

select tname,tsex,cname from teacher left join course on course.tno=teacher.tno where tsex=''

-- 43、查询最高分同学的SnoCnoDegree列。

select student.sno,cno,degree from student

join Score on Score.sno=student.sno

where degree=(select max(degree) from score);

-- 44、查询和李军同性别的所有同学的Sname.

select sname from student where ssex= --与李军同性别的同学姓名

(select ssex from student where sname='李军')--李军的性别

and sname not in('李军')--从中去除李军

-- 45、查询和李军同性别并同班的同学Sname.

select sname from student where --与李军同性别并同班的同学姓名

ssex=(select ssex from student where sname='李军')----与李军同性别的同学姓名

and class=(select class from student where sname='李军')----与李军同班的同学姓名

and sname not in ('李军');--从中去除李军

-- 46、查询所有选修计算机导论课程的同学的成绩表。

方法1select degree from score

join student on student.sno=score.sno

join course on course.cno=score.cno

where ssex='' and cname='计算机导论'

方法2select degree from score where

Sno in(select sno from student where ssex='')

and cno in(select cno from course where cname='计算机导论')

-- 47、查询出选修课程号为3-2456-166的课程的学生学号与姓名

select student.Sno,sname from student

join score on student.Sno=score.sno

where cno in('3-245','6-166')

-- 48、查询出没有选修课程号为3-2456-166的课程的学生学号与姓名

select student.Sno,sname from student

join score on student.Sno=score.sno

where cno not in('3-245','6-166')

本文来源:https://www.2haoxitong.net/k/doc/9cb0fe055a1b6bd97f192279168884868762b836.html

《最新学生选课数据库SQL语句练习题(详细分解答案).doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式