资源描述
湖南涉外经济学院 《数据库原理与应用》实验指导书
实验8 数据库综合查询
一、实验目的
1. 掌握SELECT语句的基本语法和查询条件表示方法;
2. 掌握查询条件种类和表示方法;
3. 掌握连接查询的表示及使用;
4. 掌握嵌套查询的表示及使用;
5. 了解集合查询的表示及使用。
二、实验环境
已安装SQL Server 2005的计算机;具有局域网环境,有固定IP。
三、实验学时
2学时
四、实验要求
1. 了解SELECT语句的基本语法格式和执行方法;
2. 了解连接查询的表示及使用;
3. 了解嵌套查询的表示及使用;
4. 了解集合查询的表示及使用;
5. 完成实验报告;
五、实验内容及步骤
以实验8数据为基础,请使用T-SQL 语句实现进行以下操作:
1) 查询以‘数据_’开头,且倒数第3个字符为‘结’的课程的详细情况;
select *
from course
where Cname like '数据\_%结__' escape '\';
2) 查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名;
select Cno,Cname
from course
where exists (select *
from sc
where exists (select *
from student
where course.Cno = sc.Cno and Sname like '_阳%'));
3) 列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;
select student.Sno,Sname,Sdept,course.Cno,Grade
from student,sc,course
where Cname in ('数学','大学英语') and course.Cno = sc.Cno and student.Sno = sc.Sno;
4) 查询缺少成绩的所有学生的详细情况;
select *
from student
where Sno in (select distinct Sno
from sc
where Grade IS NULL);
5) 查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
select *
from student
where Sage !=any (select Sage
from student
where Sname = '张力');
6) 查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;
select student.Sno,Sname,avg(Grade) as 平均成绩
from student inner join sc on student.sno=sc.sno
group by student.Sno,Sname
having avg(Grade) > (select avg(Grade)
from student inner join sc on student.sno=sc.sno
where sc.Sno = student.Sno and Sname = '张力');
7) 按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;
select student.Sno,Sname,Sdept,sum(case when grade>60 then ccredit end) as 已修学分
from student,sc,course
group by student.Sno,Sname,Sdept;
8) 列出只选修一门课程的学生的学号、姓名、院系及成绩;
select student.Sno,Sname,Sdept,Grade
from student,sc
where student.Sno = sc.Sno and student.Sno in(select sc.Sno
from student,sc
where student.Sno = sc.Sno
group by sc.Sno
having count(sc.Sno) = 1);
9) 查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;
select student.Sno,Sname,Cno
from student,sc
where student.Sno = sc.Sno and Cno in(select Cno
from student,sc
where Sname = '张力'and student.Sno = sc.Sno);
10) 只选修“数据库”和“数据结构”两门课程的学生的基本信息;
select S.Sno,S.Sname,S.Ssex,S.Sage,S.Sdept
from student S
where S.Sno in (select S1.Sno xuehao
from sc S1,sc S2
where S1.Sno=S2.Sno and S1.Cno=( select Cno
from course
where Cname ='数据库') and S2.Cno=(
select Cno
from course
where Cname ='数据结构'))
11) 至少选修“数据库”或“数据结构”课程的学生的基本信息;
select S.Sno,S.Sname,S.Ssex,S.Sage,S.Sdept
from student S
where S.Sno in (select S1.Sno xuehao
from sc S1,sc S2
where S1.Sno=S2.Sno and (S1.Cno=( select Cno
from course
where Cname ='数据库') or S2.Cno=(
select Cno from course where Cname ='数据结构')))
select *
from student
where Sno in (select distinct Sno
from sc
where Cno in(select Cno
from course
where Cname in ('数据库','数据结构')))
12) 列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩;
select sc.Cno,Cname,student.Sno,Sname,Grade
from sc,student,course
where student.Sno = sc.Sno and sc.Cno = course.Cno
group by sc.Cno,Cname,student.Sno,Sname,Grade;
13) 查询只被一名学生选修的课程的课程号、课程名;
select sc.Cno,Cname
from course,sc
where course.Cno = sc.Cno and sc.Cno = any ( select sc.Cno
from sc
group by sc.Cno
having count(sc.Cno) = 1 );
14) 检索所学课程包含学生‘张向东’所学课程的学生学号、姓名;
select s.Sno,Sname
from student s,sc
where s.Sno = sc.Sno and cno = all( select cno
from sc,student s
where sc.Sno = s.Sno and Sname = '张向东')
and Sname != '张向东';
15) 使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;
select Sno,Sname
from student s
where Sno in ( select Sno
from sc
where Cno in( select Cno
from course c
where cname = '数据结构' ));
16) 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;
select Sname,Sage,Sdept
from student s
where Sage < all( select Sage
from student s
where Sdept = 'CS')
and Sdept != 'CS';
17) 使用ANY、ALL 查询,列出其他院系中比CS系所有学生年龄小的学生;
select Sname,Sage,Sdept
from student s
where Sage < all( select Sage
from student s
where Sdept = 'CS')
and Sdept != 'CS';
18) 分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;
select *
from student s
where Sdept = (select Sdept
from student s
where Sname = '张力')
and Sname != '张力';
select S.Sno,S.Sname,S.Ssex,S.Sage,S.Sdept
from student S,(select Sdept
from student
where Sname='张力') SS
where S.Sdept=SS.Sdept and Sname<>'张力';
19) 使用集合查询列出CS系的学生以及性别为女的学生名单;
select *
from student
where Sdept = 'CS'
union
select *
from student
where Ssex = '女';
20) 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;
select *
from student
where Sdept = 'CS'
intersect
select *
from student
where Sage <=19;
select *
from student
where Sdept = 'CS'
except
select *
from student
where Sage <=19;
21) 使用集合查询列出选修课程1的学生集合与选修课程2的学生集合的交集;
select Sno
from sc
where Cno = '1'
intersect
select Sno
from sc
where Cno = '2';
4
展开阅读全文