- 1
- 0
- 约1.28万字
- 约 6页
- 2017-06-13 发布于河南
- 举报
Orale第7课
第7章 子查询
一.子查询分类(一个查询的结果作为另一个查询的条件)
1.无关子查询
单行子查询
多行子查询
2.相关子查询
二.详细介绍
1. 无关子查询
(1) 单行子查询
语法:select 列名 from 表名1,表名2 where 条件1 and 列名m 关系运算符(select 列名n from 表 where 条件)
例:select sname,ssex,grade from student,sc
where student.sno=sc.sno and cno=1156
and grade=(select max(grade) from sc where cno=1156)
注意:子查询的返回值必须与条件中的列名一致。子查询结果必须是固定值
例1:查询员工表中工资大于chen的员工信息
select last_name,salary from employees
where salary(select salary from employees where lower(last_name)=chen)
(2) 多行子查询
语法:select 列名 from 表名1,表名2 where 条件1 and 列名m in/any/all (select 列名n from 表 where 条件)
例:select * from employees where employee_id in
(select manager_id from employees);
注意:1.子查询的返回值及个数必须与条件中的列名及个数一致。子查询结果可以是多个值(可以是一个,但大部分是多个)
2.子查询时如果列中含有null,那么可以正常使用in,但是不能直接使用not in(必须在子查询中排除null值的范围)
题1:查询员工中谁是员工(蓝领,不是领导的)
select * from employees
where employee_id not in
(select manager_id from employees where manager_id is not null);
题2:查询工资大于(部门id为100的员工的最高工资)的员工信息。
方法一:单行子查询方式:
select * from employees
where salary(select max(salary) from employees where department_id=100);
方法二:all
select * from employees
where salaryall(select salary from employees where department_id=100)
题3:查询工资大于(部门id为100的任意员工的工资)的员工信息。
方法一:单行子查询方式:
select * from employees
where salary(select min(salary) from employees where department_id=100);
方法二:all
select * from employees
where salaryany(select salary from employees where department_id=100)
注意:
所有
nall(1,2,3) ? n3
nall(1,2,3) ? n1
任意
nany(1,2,3) ? n1
nany(1,2,3) ? n3
题3:每个系年龄最大的学生
select * from student
where (sage,sdept) in (select max(sage),sdept from student group by sdept);
2 .相关子查询(子查询中的条件中用到了外部表的信息)
语法:select 列名 表名 别名1 where 列名=(select 列名 from 表名 别名2 where 表别名1.列名=表别名2.列名)
例:select * from student a where sage=
(select max(b.sage) from student b where b.sdept=a.sdept);
题1:求每个部门的最小工资
select * from employees e
where e.salary=(select min(salary) from employees e2 where e2.department_id=e.department_id)
三. exists 和 not exists
语法:select 列名 from 表1 别名1 where exist
您可能关注的文档
- Iphne手机企业邮箱设置 九步轻松搞定.doc
- ita office2007模拟.doc
- J2E领域的一些技术框架结构图.doc
- JAV JDK 安装及环境变量配置.doc
- Jav+net笔试.doc
- Jav07-08试卷.doc
- javscript图片动态显示.doc
- Jav代码编写规范.ppt
- jav修饰符.doc
- JAV初高级阶段笔记.doc
- 2026年及未来5年内中国液体氩气行业投资前景及策略咨询研究报告.docx
- 2026年及未来5年内中国黄铜合页行业投资前景及策略咨询研究报告.docx
- 2026年及未来5年内中国竹工艺家具行业投资前景及策略咨询研究报告.docx
- 2025年中国微机数显自动分析仪市场调查研究报告.docx
- 2026年及未来5年内中国微尘白色粉笔行业投资前景及策略咨询研究报告.docx
- 2025年中国微电脑型压胶机市场调查研究报告.docx
- 2026年及未来5年内中国数字化等功游泳训练测试系统行业投资前景及策略咨询研究报告.docx
- 2025年中国圆形花瓶市场调查研究报告.docx
- 2026年及未来5年内中国植物纤维静淀过滤器行业投资前景及策略咨询研究报告.docx
- 2025年中国超音波手套机市场调查研究报告.docx
最近下载
- 农村电商(农产品电商)运营全套教学课件.pptx
- 四川省成都市第七中学2025-2026学年高一上学期11月半期考试英语(含答案).pdf
- (最新)ISO31073-2022风险管理术语(译2022-04)(推荐下载).pdf VIP
- (最新)村干部考试试题(含答案).docx VIP
- zippo图册年度机系列整合(更新至C25).docx VIP
- 多轴车铣复合加工运动转换方法:原理、实践与创新.docx
- 商务礼仪商务礼仪培训专用.ppt VIP
- 《铁路劳动安全》第04章预防机动车辆伤害.pptx VIP
- 化工制图第1章 制图基础.ppt VIP
- 化工制图AutoCAD应用基础-03章.pptx VIP
原创力文档

文档评论(0)