SQL SERVER数据库的嵌套查询实验
数据库的嵌套查询实验
实验目的:
加深对嵌套查询语句的理解。
实验内容:
使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。
实验步骤:
一. 使用带IN谓词的子查询
1. 查询与’刘晨’在同一个系学习的学生的信息:
select * from student where sdept in
(select sdept from student where sname='刘晨')
比较: select * from student where sdept =
(select sdept from student where sname='刘晨') 的异同
比较: select * from student where sdept =
(select sdept from student where sname='刘晨') andsname<>'刘晨V
比较: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname='刘晨'
2. 查询选修了课程名为’信息系统’ 的学生的学号和姓名:
SQL Server中: select sno, sname from student where sno in
(select sno from sc where cno in
(select cno from course where cname='信息系统'))
3. 查询选修了课程’1’和课程’2’的学生的学号:
select sno from student where sno in (selectsnofrom sc where cno='1')
and sno in (select sno from sc where cno='2')
比较: 查询选修了课程’1’或课程’2’的学生的sno:
select sno from sc where cno='1' or cno='2'
比较连接查询:
select A.sno from sc A, sc B where A.sno=B.sno and o='1' and o='2'
二. 使用带比较运算的子查询
4. 查询比’刘晨’年龄小的所有学生的信息:
select * from student where sage<
(select sage from student where sname='刘晨')
三. 使用带Any, All谓词的子查询
5. 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;
select sname, sage from student where sage <Any
(select sage from student where sdept='IS')
and sdept<>'IS'
6. 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:
select sname, sage from student where sage <ALL
(select sage from student where sdept='IS')
and sdept<>'IS'
7. 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄:
select sno,sname,sage from student where sage<>all
(select sage from student where sdept='CS')
四. 使用带Exists谓词的子查询和相关子查询
8. 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄:
select sno,sname,sage from student A where not exists
(select * from student B where A.sage=B.sage and A.sno<>B.sno)
9. 查询所有选修了1号课程的学生姓名:
select sname from student where exists
(select * from sc where sno=student.sno and cno='1')
10. 查询没有选修了1号课程的学生姓名:
select sname from student where not exists
(select * from sc where sno=student.sno and cno='1')
11. 查询选修了全部课程的学生姓名:
select sname from student where not exists
(select * from course where not exists
( select * from sc where sno=student.sno and cno=o))
11. 查询至少选修了学生95002选修的全部课程的学生的学号:
select distinct sno from sc A where not exists
(select * from sc B where sno='95002'and not exists
(select * from sc C where sno=A.sno and cno=o))
12. 求没有人选修的课程号cno和cnamecname:
select cno,cname from course C where not exists
(select * from sc where o=o )
13*. 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno的课程
select sno,cno from student,course where not exists
(select * from sc where cno=o and sno=student.sno)
14*. 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade):
select * from sc A where grade=
(select max(grade) from sc where sno=A.sno )
思考:
如何查询所有学生都选修了的课程的课程号cno?
…… 此处隐藏:775字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [资格考试]石油钻采专业设备项目可行性研究报告编
- [资格考试]2012-2013学年度第二学期麻风病防治知
- [资格考试]道路勘测设计 绪论
- [资格考试]控烟戒烟知识培训资料
- [资格考试]建设工程安全生产管理(三类人员安全员
- [资格考试]photoshop制作茶叶包装盒步骤平面效果
- [资格考试]授课进度计划表封面(09-10下施工)
- [资格考试]麦肯锡卓越工作方法读后感
- [资格考试]2007年广西区农村信用社招聘考试试题
- [资格考试]软件实施工程师笔试题
- [资格考试]2014年初三数学复习专练第一章 数与式(
- [资格考试]中国糯玉米汁饮料市场发展概况及投资战
- [资格考试]塑钢门窗安装((专项方案)15)
- [资格考试]初中数学答题卡模板2
- [资格考试]2015-2020年中国效率手册行业市场调查
- [资格考试]华北电力大学学习实践活动领导小组办公
- [资格考试]溃疡性结肠炎研究的新进展
- [资格考试]人教版高中语文1—5册(必修)背诵篇目名
- [资格考试]ISO9001-2018质量管理体系最新版标准
- [资格考试]论文之希尔顿酒店集团进入中国的战略研
- 全国中小学生转学申请表
- 《奇迹暖暖》17-支2文学少女小满(9)公
- 2019-2020学年八年级地理下册 第六章
- 2005年高考试题——英语(天津卷)
- 无纺布耐磨测试方法及标准
- 建筑工程施工劳动力安排计划
- (目录)中国中央空调行业市场深度调研分
- 中国期货价格期限结构模型实证分析
- AutoCAD 2016基础教程第2章 AutoCAD基
- 2014-2015学年西城初三期末数学试题及
- 机械加工工艺基础(完整版)
- 归因理论在管理中的应用[1]0
- 突破瓶颈 实现医院可持续发展
- 2014年南京师范大学商学院决策学招生目
- 现浇箱梁支架预压报告
- Excel_2010函数图表入门与实战
- 人教版新课标初中数学 13.1 轴对称 (
- Visual Basic 6.0程序设计教程电子教案
- 2010北京助理工程师考试复习《建筑施工
- 国外5大医疗互联网模式分析




