实验五:数据库综合查询
一、实验目的
1. 掌握SELECT语句的基本语法和查询条件表示方法;
2. 掌握查询条件种类和表示方法;
3. 掌握连接查询的表示及使用;
4. 掌握嵌套查询的表示及使用;
5. 了解集合查询的表示及使用。
二、实验环境
已安装SQL Server企业版的计算机(120台);
具有局域网环境,有固定IP;
三、实验学时
2学时
四、实验要求
.
.
1. 了解SELECT语句的基本语法格式和执行方法;
2. 了解连接查询的表示及使用;
3. 了解嵌套查询的表示及使用;
4. 了解集合查询的表示及使用;
5. 完成实验报告;
五、实验内容及步骤
1.利用Transact-SQL嵌套语句实现下列数据查询操作。
1) 查询选修了计算机体系结构的学生的基本信息。
select * from student where 学号 in (select 学号 from sc where 课程号 in (select 课程号 from course where 课程名称 = '计算机体系结构'))
2) 查询年龄比李勇小的学生的学号和成绩。
select sc.学号,成绩 from sc,student where sc.学号=student.学号 and student.年龄 < (select 年龄 from student where 姓名='李勇')
3)查询其他系中比系编号为‘D1’的学生中年龄最小者要大的学生的信息。
.
.
select * from student where 年龄 in ( select 年龄 from student where 年龄 > any (select 年龄 from student where 系编号='d1' )) and 系编号!='d1'
4) 查询其他系中比系编号为‘D3’的学生年龄都大的学生的姓名。
select 姓名 from student where 年龄 in ( select 年龄 from student where 年龄 > all (select 年龄 from student where 系编号='d3' )) and 系编号!='d3'
5) 查询‘C1’课程的成绩高于70的学生姓名。
select 姓名 from student ,sc
Where student.学号 = sc.学号 and sc.成绩 >70 and sc.课程号 = 'C1'
6) 查询‘C1’课程的成绩不高于70的学生姓名。
select 姓名 from student ,sc
where student.学号 = sc.学号 and sc.成绩 <=70 and sc.课程号 = 'C1';
7) 查询没有选修的学生姓名。
select 姓名 from student
where 学号 not in (select 学号 from sc )
.
.
8)查询学校开设的课程总数。
select count(课程号) 课程号_count from course;
9)查询选修两门及两门以上课程的学生姓名。
select 姓名 from student
where 学号 in (select 学号 from sc
group by 学号
having count(课程号)>=2 );
10)查询开设的课程和选修该课程的学生的总成绩、平均成绩、最高成绩和最低成绩。
select 课程号,sum(成绩) sum_成绩,avg(成绩) avg_成绩,max(成绩) max_成绩,min(成绩) min_成绩 from sc
group by 课程号;
(二)、以数据库原理实验4数据为基础,请使用T-SQL 语句实现进行以下操作:
1. 查询以‘DB_’开头,且倒数第3个字符为‘s’的课程的详细情况;
select * from course
.
.
where 课程名称 like 'DB\\_%s__' escape '\\';
2. 查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名;
select a.姓名,a.sno,b.cno,b.课程名称 from student a,course b,sc c
where a.sno=c.sno and c.cno=b.cno and a.姓名 like '_阳%';
3. 列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;
select a.sno,a.姓名,a.sdept,b.cno,b.grade from student a,sc b
where a.sno = b.sno and b.cno in (
select cno from course where 课程名称 = '数学' or 课程名称 = '大学英语' );
4. 查询缺少成绩的所有学生的详细情况;
select * from student
where sno in (
select sno from sc where grade is null );
5. 查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
.
.
select a.* from student a,student b
where b.姓名='张力' and a.年龄<>b.年龄;
6. 查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;★
select a.sno,a.姓名,avg(b.grade) avg_grade from student a,sc b
where a.sno=b.sno
group by a.sno,a.姓名
having avg(b.grade)>(
select avg(grade) from sc where sno = (
select sno from student where 姓名 = '张力'
)
7. 按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;★
select student.sno 学号,student.姓名 姓名,student.sdept 所在院系 ,sum(course.ccredit) 已修学分 from student,sc,course
.
.
where student.sno = sc.sno and sc.cno = course.cno and
sc.grade>=60
group by student.sno,student.姓名 ,student.sdept;
8. 列出只选修一门课程的学生的学号、姓名、院系及成绩;
select a.sno,a.姓名,a.sdept,b.grade from student a,sc b
where a.sno=b.sno and b.sno in (
select sno from sc
group by sno
having count(sno)=1
);
9. 查询选修“数据库”或“数据结构”课程的学生的基本信息;
select student.* from student,course,sc
where student.sno=sc.sno and sc.cno=course.cno
.
.
and (course.课程名称 = '数据库' or course.课程名称 = '数据结构');
10. 列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩;
select a.sno,a.姓名,b.cno,b.课程名称,c.grade
from student a,course b,sc c
where a.sno = c.sno and b.cno = c.cno;
11. 查询只被一名学生选修的课程的课程号、课程名;
select cno,课程名称 from course
where cno in (
select cno from sc
group by cno
having count(sno)=1
);
12. 检索所学课程包含学生‘张向东’所学课程的学生学号、姓名;★
.
.
select sno,姓名 from student
where sno in (
select sno from sc
where cno in (
select cno from student,course
where 姓名 = '张向东'
)
);
13. 检索所学课程包含学生‘张向东’所学全部课程的学生学号、姓名;★
select student.sno,姓名 from student
where not exists (
select a.sno from sc a
where a.sno = (
.
.
select sno from student
where 姓名 = '张向东' and not exists (
select b.sno from sc b
where b.sno = student.sno and a.cno = b.cno
)
)
);
14. 使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;
select sno,姓名 from student
where sno in (
select sno from sc
where cno in (
select cno from course
.
.
where 课程名称 = '数据结构'));
15. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;
select 姓名,年龄,sdept from student
where sdept <> 'CS'and 年龄 < (
select max(年龄) from student
where sdept = 'CS'
);
16. 使用ANY、ALL 查询,列出其他院系中比CS系所有学生年龄小的学生;
--------any查询--------
select * from student
where sdept <> 'CS' and
年龄 < any (
select min(年龄) from student
.
.
where sdept = 'CS'
);
--------all查询---------
select * from student
where sdept <> 'CS' and
年龄 < all (
select 年龄 from student
where sdept = 'CS'
);
17. 分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;
--------------连接查询-------------
select a.* from student a,student b
where b.姓名 = '张力' and a.sdept = b.sdept and a.姓名 <> '张力';
.
.
--------------嵌套查询--------------
select * from student
where 姓名 <> '张力' and sdept in (
select sdept from student
where 姓名 = '张力');
18. 使用集合查询列出CS系的学生以及性别为女的学生名单;
select * from student
where sdept = 'CS'
union
select * from student
where ssex = '女';
19. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;
--------交集---------
.
.
select * from student
where sdept = 'CS'
intersect
select * from student
where 年龄 <= 19;
--------差集---------
select * from student
where sdept = 'CS'
except
select * from student
where 年龄 <= 19;
20. 使用集合查询列出选修课程1的学生集合与选修课程2的学生集合的交集;
select * from sc
.
.
where cno = '1'
intersect
select cno from sc
where cno = '2'
六、出现问题及解决办法
如:某些查询操作无法执行,如何解决?
.
因篇幅问题不能全部显示,请点此查看更多更全内容