实验过程及内容:
- 查询所有选课学生的姓名和选修课程的课程编号。
Select 课程编号,姓名
From student join score on student.学号=score.学号;
- 查询所有选课学生的姓名、课程名称和成绩。
Select score.成绩,student.姓名,course.课程名称
From score join student on student.学号=score.学号
join course on course.课程编号=score.课程编号;
- 查询所有学生的姓名、课程名称、成绩,包括未选课的同学(即选课的同学显示姓名、课程名称、成绩,未选课的同学只显示姓名)。
Select student.学号,student.姓名,course.课程编号
from student left join score on student.学号=score.学号
left join course on score.课程编号=course.课程编号;
- 查询所有选修了“管理学”课程的学生名单。
Select *
From score,course
Where score.课程编号=course.课程编号
AND course.课程名称='管理学';
- 查询同时选修了“管理学”和“计算机文化基础”两门课程的学生名单。
Select score.学号,student.姓名
From score,course,student
Where score.学号=student.学号
and score.课程编号=course.课程编号
and course.课程名称='管理学'
INTERSECT
Select score.学号,student.姓名
From score,course,student
where score.学号=student.学号
and score.课程编号=course.课程编号
and course.课程名称='计算机文化基础';
- 查询选修了“管理学”但没选修“计算机文化基础”课程的学生名单。
Select score.学号,student.姓名
From score,course,student
Where score.学号=student.学号
and score.课程编号=course.课程编号
and course.课程名称='管理学'
EXCEPT
Select score.学号,student.姓名
From score,course,student
where score.学号=student.学号
and score.课程编号=course.课程编号
and course.课程名称='计算机文化基础';
- 查询所有入学成绩高于平均入学成绩的学生名单和入学成绩。
Select student.姓名,student.入学成绩
From student
where student.入学成绩>(select AVG(入学成绩) from student);
- 查询所有党员及选修04010101课程的学生的学号。
Select student.学号,student.党员否
From student,score
Where student.党员否=1
and student.学号=score.学号
and score.课程编号=04010101;
- 查询入学成绩高于所有男同学入学成绩的女生姓名。
Select student.姓名
From student
Where student.性别='女'
and student.入学成绩>all(Select 入学成绩 from student where 性别 ='男')
- 查询入学成绩高于任意一名女同学入学成绩的男生姓名。
Select student.姓名
From student
Where student.性别='男'
and student.入学成绩>any(Select 入学成绩 from student where 性别='女');
- 查询所有开课课程的课程名称及先修课名称,显示课程名称和先修课两列(只显示有先修课的课程)。
Select first.课程名称,second.课程名称 先修课
From course first,course second
Where first.先修课 is not null and first.先修课=second.课程编号;
- 查询2015094002同学与2015094001同学都选修且2015094002同学比2015094001同学成绩高的选修课程的编号。
Select 课程编号
From score second
Where second.学号=2015094001
and second.成绩< (Select 成绩 from score where second.课程编号=score.课程编号 and score.学号=2015094002);
- 查询至少与2015094001同学选修了同一门课程的学生名单。
Select distinct student.姓名
from student join score on student.学号=score.学号
where exists
(select *
from score x
where x.学号='2015094001' and
not exists
(select *
from score y
where y.学号=x.学号 and
y.学号=score.学号));
- 查询选修了全部课程的学生名单。
Select 姓名
from student
where not exists
(select *
from course
where not exists
(select *
from score
where 学号=student.学号
and 课程编号=course.课程编号));
- 查询选修了学号为“2015094003”同学选修的全部课程的学生名单(不包括本人)。
Select distinct student.姓名
from student join score on student.学号=score.学号
where 课程编号 in
(select 课程编号
from score
where 学号='2015094003')
and student.学号<>'2015094003';
- 查询国际贸易081班男生人数和女生人数及合计信息。
Select isnull(性别,'合计') 性别,count(*) as 人数
from student join class on student.班级编号=class.班级编号
where class.班级名称='国际贸易'
group by 性别 with rollup;
- 进行分段统计,显示为每门课程良好以上及以下的学生人数。(假设良好是指成绩高于80分)
Select 课程编号,
良好以上=(select count(*) from score as x
where x.成绩>=80 and x.课程编号=score.课程编号),
良好以下=(select count(*) from score as x
where x.成绩<=80 and x.课程编号=score.课程编号),
count(*) as 合计
from score
group by 课程编号