本实验基于school数据库实现
点击察看school数据库说明
一、实验目的
1.掌握Management Studio的使用。
2.掌握带函数查询和综合查询的使用。
二、实验内容及要求
1.统计年龄大于30岁的学生的人数。
select count(*) as '大于30岁的人数'
from Student
where (year(getdate())-year(birthday))>30
2.统计数据结构有多少人80分或以上。
select count(*)
from studentgrade
where grade>=80 and course_id =
(select course_id
from course
where course_name = '数据结构' )
3.查询“0203”课程的最高分的学生的学号。(请分别用TOP1和函数来进行查询,并分析它们的区别)
select top 1 stu_id
from studentgrade
where course_id = '0203'
order by grade desc
select stu_id
from studentgrade
where grade = (select max(grade)
from studentgrade
where course_id = '0203')
区别:因为最高分不止一个,第一种方法只能查一个,第二种方法可以查所有最高分的学生
4.统计各系开设班级的数目(系名称、班级数目),并创建结果表。(需考虑没有班级的系)
select depar_name,count(class_id) as '班级数目'
from deparment left join class on
deparment.depar_id = class.depar_id
group by depar_name
5.选修了以“01”开头的课程的学生学号,姓名,选课的课程号。
select distinct student.stu_id,stu_name,course_id
from student,studentgrade
where course_id in (select course_id
from studentgrade
where course_id like '01%')
6.统计每科目的最高分、最低分,平均分、总分,并以中文列名显示。
select course_id,max(grade) as '最高分',min(grade) as '最低分',avg(grade) as '平均分',sum(grade) as '总分'
from studentgrade
group by course_id
7.所有成绩都在70分以上的学生姓名(提示:使用子查询。需考虑未选课的学生)。
子查询
select stu_id,stu_name
from student
where stu_id in (select stu_id
from studentgrade
group by stu_id
having min(grade) >= 70)
连接查询
select student.stu_id,stu_name
from student,studentgrade
where student.stu_id = studentgrade.stu_id
group by student.stu_id,stu_name having min(grade) >= 70
8.“数据库”课程得最高分的学生的学号、姓名和所在系(提示:使用子查询)。
select student.stu_id,stu_name,depar_name
from student,deparment,class,studentgrade,course
where student.class_id = class.class_id and
class.depar_id = deparment.depar_id and
student.stu_id = studentgrade.stu_id and
studentgrade.course_id = course.course_id and
grade = (select max(grade)
from studentgrade
where course_id = (select course_id
from course
where course_name = '数据库'))
9.至少选修了两门课及以上的学生姓名和性别。
select stu_name,stu_sex
from student
where stu_id in (select stu_id
from studentgrade
group by stu_id having count(*)>=2)
如有错误请指出!!!