数据库原理及应用(SQL Server 2008版)
第5章实验数据查询
(1) 列出所有姓“李”且全名为3个汉字学生的姓名、性别、和出生日期 Sql文本:select sname 姓名,ssex 性别,sbirthday 出生日期 from student
where sname like '李__'
提示:参考教材P128例5-21
(2) 显示在1989年以后出生的学生的基本信息、选修课程号及课程成绩 Sql语句:select sname 姓名,ssex 性别,sbirthday 出生日期 , grade.c_id,grade
from student join grade on student.s_id=grade.s_id where sbirthday>'1989-1-1'
提示:参考教材P141例5-49
(3) 列出选修了1号课程学生的学号,并按成绩降序排列 SQL语句:select s_id 学号,grade 成绩 from grade where c_id=1
order by grade desc
提示:参考教材P130例5-28
(4) 显示student表中的学生总数,在结果集中列标题中显示学生总人数 SQL语句:select COUNT(*) 学生总人数 from student
提示:参考教材P132例5-33
(5) 显示所有专业(要求不重复,不包括空值),并在结果集中增加一列字段“专业规模”用来记录每个专业的学生人数
SQL语句:select smajor,COUNT(*) 专业规模 from student
where smajor is not null group by smajor
提示:参考教材P133例5-37
(6) 显示选修的课程门数大于3的各个学生的学号及其选修课程数 Sql语句:select s_id 学号,COUNT(*) 选修课程数 from grade group by s_id
having COUNT(*)>3
提示:参考教材P135例5-39
(7) 列出有两门以上课程(含两门)不及格的学生的学号及该学生的平均成绩。 SQL语句:select s_id,AVG(grade) from grade
where s_id in (select s_id from grade
where grade<60 group by s_id
having COUNT(*)>=2) group by s_id
(8) 列出同时选修1号课程和2号课程所有学生的学号此题较难,选做 SQL语句:select s_id from grade where c_id in
(select c_id from grade where c_id=1 or c_id=2) group by s_id
having COUNT(*)=2
(9) 查询所选课程的平均成绩大于学号2010190001学生平均成绩的学号和平均成绩 SQL语句:select s_id 学号,AVG(grade) 平均成绩 from grade group by s_id
having AVG(grade)>(
select AVG(grade) from grade where s_id=2010190001
提示:参考教材P144例5-55
(10) 按照“学号,姓名,院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和 SQL语句select
student.s_id 学号, sname 姓名, sdepartment 院系,SUM(course.credit) from student,course,grade where grade>60
and student.s_id=grade.s_id and grade.c_id=course.c_id group by student.s_id,sname,sdepartment
(11) 查询只被一名学生选修的课程的课程号、课程名 SQL语句:select c_id,cname from course
where c_id in(select c_id from grade group by c_id
having COUNT(*)=1 )
提示:参考教材P145例5-56
(12) 使用嵌套查询查询其他院系中年龄小于“信息工程学院”的某个学生的学生姓名、年龄和院系。
SQL语句:select 姓名=sname,年龄=YEAR(GETDATE())-YEAR(sbirthday),
院系=sdepartment from student
where (YEAR(GETDATE())-YEAR(sbirthday))>any (
select YEAR(GETDATE())-YEAR(sbirthday) from student
where sdepartment='信息工程学院' )
and sdepartment<>'信息工程学院' or sdepartment is null
提示:参考教材P146例5-59
(13) 使用集合查询列出“信息工程学院”的学生以及性别为女的学生学号及姓名 SQL语句:select s_id,sname from student
where sdepartment ='信息工程学院' union
select s_id,sname from student where ssex='女'
提示:参考教材P151例5-66
(14) 使用集合查询列出“信息工程学院”的学生与年龄不大于20岁的学生的交集和差集。
差集:select s_id,sname from student
where sdepartment ='信息工程学院' except
select s_id,sname from student
where YEAR(GETDATE())-YEAR(sbirthday)<20 交集select s_id,sname from student
where sdepartment ='信息工程学院' intersect
select s_id,sname from student
where YEAR(GETDATE())-YEAR(sbirthday)<20
提示:参考教材P152例5-68、例5-69
难度较低题
(15) 查询数据表student中所有学生的姓名、性别和年龄,并按年龄降序排列 Sql文本:select 姓名=sname,性别=ssex,年龄=YEAR(GETDATE())-YEAR(sbirthday) from student
order by 年龄 desc
提示:参考教材P123例5-5
(16) 查询学生的所有不重复的专业名称 SQL文本:select distinct smajor as 专业名称 from student
提示:参考教材P124例5-6
(17) 查询数据表student中年龄最大的5名学生的姓名、生日 SQL文本:select top 5 姓名=sname,生日=sbirthday from student
order by YEAR(GETDATE())-YEAR(sbirthday) desc
提示:参考教材P124例5-7(先按生日排序)
(18) 查询course表中学时数在60~100之间的课程信息 SQL文本:select * from course where chours between 60 and 100
提示:参考教材P127例5-15
(19) 查询外语学院、信息工程学院、数学工程学院的学生信息 SQL文本:select * from student
where sdepartment in ('外语学院','信息工程学院','数学工程学院')
提示:参考教材P127例5-17
(20) 查询目前暂时没有专业的学生信息 SQL文本:select * from student
where smajor is null
提示:参考教材P127例5-19
(21) 查询学生的学号及其选修课程平均分 SQL文本:select s_id,AVG(grade) as 平均分 from grade group by s_id
提示:参考教材P133例5-36
(22) 查询学生的学号、姓名及其选修课程数
SQL语句:select student.s_id as 学号,sname as 姓名,count(*) as 选修课程数 from grade inner join student on grade.s_id=student.s_id group by student.s_id,sname
提示:参考教材P134例5-38(先将student表和grade表连接,参考P141例5-49)
(23) 查询有先修课要求课程的课程编号、课程名称及其先修课程编号、先修课程名称 SQL语句select a.c_id 课程编号,a.cname 课程名称, a.cp_id 先修课程编号,b.cname 先修课程编号 from course a join course b on b.c_id=a.cp_id
提示:参考教材P142例5-50
(24) 查询学生“张华”选修的课程编号和成绩 SQL语句:select c_id,grade from grade
where s_id=(select s_id from student where sname='张华')
提示:参考教材P144例5-53
(25) 利用嵌套查询技术查询有选课的学生学号和姓名 SQL语句:select s_id 学号,sname 姓名 from student where s_id in (select s_id from grade )
提示:参考教材P145例5-56