S2 SQL测试题

来源:百度知道 编辑:UC知道 时间:2024/06/18 03:27:32
学生成绩信息三个表,结构如下:
学生表:Member
字段名称 数据类型 说明
MID Char(10) 学生号,主键
MName Char(50) 姓名
课程表:
字段名称 数据类型 说明
FID Char(10) 课程,主键
FName Char(50) 课程名
成绩表:Score
字段名称 数据类型 说明
SID int 自动编号,主键,成绩记录号
FID Char(10) 课程号,外键
MID Char(10) 学生号,外键
Score int 成绩
请编写T-SQL语句来实现如下功能:
1) 查询各个学生语文、数学、英语、历史课程成绩,例如下表:
姓名 语文 数学 英语 历史
张萨 78 67 89 76
王强 89 67 84 96
李三 70 87 92 56
李四 80 78 97 66
2) 查询四门课中成绩低于70分的学生及相对应课程名和成绩。
3) 统计各个学生参加考试课程的平均分,且按平均分数由高到底排序。
4) 创建存储过程,分别查询参加1、2、3、4门考试及没有参加考试的学生名单,要求显示姓名、学号。

第一题给你做完了.
select f.mname,
f.语文,
f.数学,
f.英语,
f.历史 from
(select MName
,
语文=sum(case when FID='F001' then score end),
数学=sum(case when FID='F002' then score end),
英语=sum(case when FID='F003' then score end),
历史=sum(case when FID='F004' then score end)
from dbo.Member inner join dbo.score on dbo.Member.MID=dbo.score.MID
group by MName
) f
where f.数学 is not null and f.语文 is not null and f.英语 is not null and f.历史 is not null
第二题
select MName
,
语文=sum(case when FID='F001' then score end),
数学=sum(case when FID='F002' then score end),
英语=sum(case when FID='F003' then score end),
历史=sum(case when FID='F004' then score end)
from dbo.Member inner join dbo.score on dbo.Member.MID=dbo.score.MID
where score<70
group by MName
第三题
select 学生姓名=f.mname,
f.语文,
f.数学,