Oracle子查询(嵌套查询)
概念:
所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句
需求:查找和Smith同部门的所有员工的id和last_name
目标: 员工id,last_name
from: s_emp
条件: s_emp.dept_id = Smith所在部门的id?
select id,last_name
from s_emp
where dept_id = ?
阶段目标: Smith所在部门的id
目标: dept_id
from : s_emp
条件: last_name = 'Smith';
select dept_id
from s_emp
where last_name = 'Smith';
组合:
select id,last_name
from s_emp
where dept_id = (
select dept_id
from s_emp
where last_name = 'Smith'
)
应用场景:
1.一条查询语句的查询条件依赖另外一条查询语句的查询结果。
2.一条查询语句的查询结果是作为另外一条查询语句的查询表(查询依据)。
3.在DML操作中使用子查询(后期介绍)
子查询的基本原则:
1.在查询中可以有单行子查询和多行子查询
2.子查询可以出现在操作符的左边或者右边
3.子查询在很多sql命令中都可以使用
4.嵌套查询先执行,然后将结果传递给主查询。
一、比较值不确定,需要另外一个select语句执行后才能得到,使用子查询
语法:
select ...
from ...
where col_name 比较操作符 (
select ...
from ...
where ...
group by ...
having...
)
group by ...
having...
order by ...
单值子查询:子查询的结果为1个
需求:
1.查询和Simith职称相同的所有员工的last_name和职称
分析步骤:
1.确定最终查询结果(目标/主查询):查询员工的last_name和title
from : s_emp
条件 : title = Smith的职称
select last_name,title
from s_emp
where title = ?
2.确定条件(子查询):Smith的职称
from : s_emp
条件 :last_name = 'Smith';
select title
from s_emp
where last_name = 'Smith';
3.组合
select last_name,title
from s_emp
where title = (select title
from s_emp
where last_name = 'Smith');
2.查看工资大于Chang员工工资的所有员工的id和名字。
最终目标:员工的id,last_name
from:s_emp
条件: salary > Chang员工的工资
select id,last_name
from s_emp
where salary > ?
阶段目标:Chang员工的工资
from : s_emp
条件: last_name = 'Chang';
select salary
from s_emp
where last_name = 'Chang';
组合:
3.查看员工工资小于平均工资的所有员工的id和名字
例如:查找和Smith同一个部门的员工的id和last_name
多值子查询:子查询的结果为多个
需求:
1.查询所在区域为2号区域的所有部门的员工的id和last_name
1.确定最终查询结果: 员工的id, last_name
from : s_emp
条件 :s_emp.dept_id in (?);
select id,last_name
from s_emp
where dept_id in ?
2.确定条件:所在区域为2号部门id
子查询:部门id
from : s_dept
条件: region_id = 2;
select id
from s_dept
where region_id = 2;
3.组合:
select id,last_name
from s_emp
where dept_id in (
select id
from s_dept
where region_id = 2
)
子查询出现情况二:
查找的内容不确定,需要从构建出来一个查询的表
语法:
select ....
from (select .... from ....) b
where ......
练习:查询各部门的id,name 和部门员工的平均工资
1.查询目标:
需要部门的id,部门的name ------ 从 s_dept表中
部门员工的平均工资 avg(salary) --------- salary只有s_emp表中有
条件 : 部门id,name和部门 员工,因此要求部门的id跟员工所在部门的id相等才连接
select id,name, 平均工资
from s_dept , ?
where s_dept.id = ?.dept_id;
2.查询条件
select(dept_id,avg(salary) sal)
from s_emp
group by dept_id;
3.组合:
select id,name,b.sal
from s_dept dept,(select dept_id,avg(salary) sal
from s_emp
group by dept_id
) b
where dept.id = b.dept_id;
Oracle子查询(嵌套查询)的更多相关文章
- SQL子查询/嵌套查询
sql子查询 嵌套SELECT语句 嵌套SELECT语句也叫子查询,一个 SELECT 语句的查询结果能够作为另一个语句的输入值.子查询不但能够出现在Where子句中,也能够出现在from子句中,作为 ...
- 关于mysql中的数据查询—嵌套查询
嵌套查询 一个SELECT FROM WHERE语句称为一个查询块. 嵌套查询:将一个查询块嵌套在另一个查询块的WHERE子句或者HAVING短语的条件中的查询. 注:子查询的SELECT语句中不 ...
- mongodb多个条件查询in,日期查询,嵌套查询,统计集合总数等常用实例
1. 多个条件查询in in db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) 2. 日期查询 db.books.find({}) 查询时间大于6-,结 ...
- 数据库SQL语言学习--上机练习2(连接查询 嵌套查询)
上机练习2 1. 启动SQL Server 2008中的 SQL Server Management Studio. 2. 针对下面三张基本表进行操 ...
- SQL——嵌套查询与子查询
前言 sql的嵌套查询可以说是sql语句中比较复杂的一部分,但是掌握好了的话就可以提高查询效率.下面将介绍带in的子查询.带比较运算符的子查询.带any/all的子查询.带exists的子查询以及基于 ...
- SQL Server 之 子查询与嵌套查询
当由where子句指定的搜索条件指向另一张表时,就需要使用子查询或嵌套查询. 1 子查询 子查询是一个嵌套在select.insert.update或delete语句或其他子查询中的查询.任何允许使用 ...
- MySQL——优化嵌套查询和分页查询
优化嵌套查询 嵌套查询(子查询)可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查询中.嵌套查询写起来简单,也容易理解.但是,有时候可以被更有效率的连接(JOIN ...
- SQL连接查询和嵌套查询详解
连接查询 若一个查询同时涉及两个或两个以上的表,则称之为连接查询.连接查询是数据库中最最要的查询, 包括: 1.等值连接查询 2.自然连接查询 3.非等值连接查询 4.自身连接查询 5.外连接查询 6 ...
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...
随机推荐
- IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)
*****HMViewController.m #import "HMViewController.h" #import "HMFriendsGroupModel.h&q ...
- Rearrange a string so that all same characters become d distance away
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearra ...
- 【iCore3 双核心板_FPGA】实验十九:基于双口RAM的ARM+FPGA数据存取实验
实验指导书及代码包下载: http://pan.baidu.com/s/1pLReIc7 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- Python强化训练笔记(一)——在列表,字典,集合中筛选数据
列表,字典,集合中根据条件筛选数据,如下所示 列表:[-10,2,2,3,-2,7,6,9] 找出所有的非负数 字典:{1:90,2:55,3:87...} 找出所有值大于60的键值对 集合:{2,3 ...
- 一些asp.net使用
1.英文格式的日期转为中文格式 eg:string englishDate="03-04-2012"; string chineseDate=Regex.replace(engli ...
- Any changes made by a writer will not be seen by other users of the database until the changes have been completed
https://en.wikipedia.org/wiki/Multiversion_concurrency_control Multiversion concurrency control (MCC ...
- Where is the Global.asax.cs file
I am using VS 2008. I have created a new Asp.net web site project from File->New->Website-> ...
- UIView添加手势
_shareImage.userInteractionEnabled=YES; UITapGestureRecognizer *imagegesture=[[UITapGestureRecognize ...
- 连接mysql问题 mysqlnd cannot connect to MySQL 4.1+ using old authentication
第一篇:PHP5.3开始使用MySqlND作为默认的MySql访问驱动,而且从这个版本开始将不再支持使用旧的用户接口链接Mysql了,你可能会看到类似的提示: #2000 - mysqlnd cann ...
- mysql数据库、表、字段、记录:增、删、改、查
/* 结构:数据库.表.字段.记录 操作:增删改查 */ -- 1.数据库:增删改查 create datebase if not exists jkxy; drop database if exis ...