实验六 带函数查询和综合查询(1)
一、实验目的
1.掌握Management Studio的使用。
2.掌握带函数查询和综合查询的使用。
二、实验内容及要求
1统计年龄大于30岁的学生的人数。
select count(*)
from student
where year(getdate())-year(birthday)>30
2统计数据结构有多少人80分或以上。
select count(*)
from studentgrade,course
where course_name='数据结构'
and course.course_id=studentgrade.course_id
and grade>=80
3 查询“0203”课程的最高分的学生的学号。(请分别用TOP1和函数来进行查询,并分析它们的区别)
方法一
select stu_id
from studentgrade
where grade=(
select max(grade)
from studentgrade
where course_id='0203')
and course_id='0203'
方法二
select top 1 stu_id
from studentgrade
where course_id='0203'
order by grade desc
4 统计各系开设班级的数目(系名称、班级数目),并创建结果表。(需考虑没有班级的系)
select depar_name,count(class_id) as 班级数目
into newtable1
from deparment left outer join class on(deparment.depar_id=class.depar_id)
group by depar_name
5 选修了以“01”开头的课程的学生学号,姓名,选课的课程号。
select student.stu_id,stu_name,course_id
from student,studentgrade
where course_id like '01%'
and student.stu_id=studentgrade.stu_id
6 统计每科目的最高分、最低分,平均分、总分,并以中文列名显示。
select course_name 课程,max(grade) 最高分,min(grade) 最低分,avg(grade) 平均分,sum(grade) 总分
from studentgrade,course
where studentgrade.course_id=course.course_id
group by course_name
7 所有成绩都在70分以上的学生姓名(提示:使用子查询。需考虑未选课的学生)
方法一:连接查询(group by student.stu_id不能省略stu_id,否则同名的“涂杰杰”只出现一次)
select student.stu_id,stu_name /*student.stu_id可省略*/
from student,studentgrade
where student.stu_id=studentgrade.stu_id
group by student.stu_id,stu_name
having min(grade)>=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,grade
from student,studentgrade
where student.stu_id=studentgrade.stu_id
8 “数据库”课程得最高分的学生的姓名、性别和所在系(提示:使用子查询)
方法一:/*top 1只能显示第一个最高分,不正确*/
select top 1 stu_name, stu_sex,depar_name
from student,deparment,class,course,studentgrade
where deparment.depar_id=class.depar_id
and class.class_id=student.class_id
and student.stu_id=studentgrade.stu_id
and studentgrade.course_id=course.course_id
and course_name='数据库'
order by grade desc
方法二:/*top 1只能显示第一个最高分,不正确*/
select stu_name, stu_sex,depar_name
from student,deparment,class
where deparment.depar_id=class.depar_id
and class.class_id=student.class_id
and stu_id=(select top 1 stu_id
from studentgrade
where course_id=(select course_id
from course
where course_name='数据库' )
order by grade desc)
方法3:
select student.stu_id,stu_name, stu_sex,depar_name
from student,deparment,class,studentgrade
where deparment.depar_id=class.depar_id
and class.class_id=student.class_id
and student.stu_id=studentgrade.stu_id
and grade= (select max(grade)
from studentgrade
where course_id=(select course_id
from course
where course_name='数据库' )
)
/*错误,不是数据库的最高分,如果和数据库的最高分相同的人也会出现,为什么?*/
select student.stu_id,stu_name, stu_sex,depar_name
from student,deparment,class,studentgrade,course
where deparment.depar_id=class.depar_id
and class.class_id=student.class_id
and student.stu_id=studentgrade.stu_id
and course.course_id=studentgrade.course_id
and grade= (select max(grade)
from studentgrade
where course_id=(select course_id
from course
where course_name='数据库' )
)
and course_name='数据库'
方法 4:
select student.stu_id,stu_name, stu_sex,depar_name
from student,deparment,class,studentgrade,course
where deparment.depar_id=class.depar_id
and class.class_id=student.class_id
and student.stu_id=studentgrade.stu_id
and course.course_id=studentgrade.course_id
and grade= (select max(grade)
from studentgrade,course
where studentgrade.course_id=course.course_id
and course_name='数据库'
)
and 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)