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: . 查看表的 ...
随机推荐
- 【iCore3 双核心板_ uC/OS-III】例程一:认识 uC/OS-III
实验指导书及代码包下载: http://pan.baidu.com/s/1i4FuMep iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 50分钟学会Laravel 50个小技巧
50分钟学会Laravel 50个小技巧 时间 2015-12-09 17:13:45 Yuansir-web菜鸟 原文 http://www.yuansir-web.com/2015/12/09 ...
- Mac OS X 懒人版安装教程(之前的图全部挂了,所以重发了)
请版主把我之前发的那个帖子删了!因为所有的图全部挂了,所以麻烦版主了…… 安装中出现五国的话就请进入这里看看是那里的错误http://bbs.pcbeta.com/viewthread-863656- ...
- C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...
- Extjs 表格横坐标显示问题
在项目中显示chart时,当横坐标的标签名称过长时,extjs会自动隐藏部分的标签. 我想,如果能让标签斜着,或者纵向显示的话,就能够节省x轴上的长度. 经过在网上查找,解决方案如下. //在表格的a ...
- 去掉comments
三种comments: /* Test program */ int main() { // variable declaration int a, b, c; /* This is a test m ...
- iOS中的#ifdef DEBUG为什么会在didFinishLaunchingWithOptions之前执行
#ifdef DEBUG ...程序段1... #else ...程序段2... #endif 这表明如果标识符DEBUG已被#define命令定义过则对程序段1进行编译:否则对程序段2进行编译.#i ...
- luagd介绍
luagd 官网: http://ittner.github.io/lua-gd/ 下载 http://files.luaforge.net/releases/lua-gd/lua-gd/lua-gd ...
- C# 窗体
窗体的事件:删除事件:先将事件页面里面的挂好的事件删除,再删后台代码里面的事件 Panel是一个容器 1.Label -- 文本显示工具Text:显示的文字取值.赋值:lable1.Text 2.Te ...