概念:

所谓子查询,即一个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子查询(嵌套查询)的更多相关文章

  1. SQL子查询/嵌套查询

    sql子查询 嵌套SELECT语句 嵌套SELECT语句也叫子查询,一个 SELECT 语句的查询结果能够作为另一个语句的输入值.子查询不但能够出现在Where子句中,也能够出现在from子句中,作为 ...

  2. 关于mysql中的数据查询—嵌套查询

    嵌套查询 一个SELECT  FROM  WHERE语句称为一个查询块. 嵌套查询:将一个查询块嵌套在另一个查询块的WHERE子句或者HAVING短语的条件中的查询. 注:子查询的SELECT语句中不 ...

  3. mongodb多个条件查询in,日期查询,嵌套查询,统计集合总数等常用实例

    1. 多个条件查询in in db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) 2. 日期查询 db.books.find({}) 查询时间大于6-,结 ...

  4. 数据库SQL语言学习--上机练习2(连接查询 嵌套查询)

    上机练习2 1.              启动SQL Server 2008中的 SQL Server Management Studio. 2.              针对下面三张基本表进行操 ...

  5. SQL——嵌套查询与子查询

    前言 sql的嵌套查询可以说是sql语句中比较复杂的一部分,但是掌握好了的话就可以提高查询效率.下面将介绍带in的子查询.带比较运算符的子查询.带any/all的子查询.带exists的子查询以及基于 ...

  6. SQL Server 之 子查询与嵌套查询

    当由where子句指定的搜索条件指向另一张表时,就需要使用子查询或嵌套查询. 1 子查询 子查询是一个嵌套在select.insert.update或delete语句或其他子查询中的查询.任何允许使用 ...

  7. MySQL——优化嵌套查询和分页查询

    优化嵌套查询 嵌套查询(子查询)可以使用SELECT语句来创建一个单列的查询结果,然后把这个结果作为过滤条件用在另一个查询中.嵌套查询写起来简单,也容易理解.但是,有时候可以被更有效率的连接(JOIN ...

  8. SQL连接查询和嵌套查询详解

    连接查询 若一个查询同时涉及两个或两个以上的表,则称之为连接查询.连接查询是数据库中最最要的查询, 包括: 1.等值连接查询 2.自然连接查询 3.非等值连接查询 4.自身连接查询 5.外连接查询 6 ...

  9. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

随机推荐

  1. 把验证码和生成时间负值给$_SESSION[vCode]生成图像给浏览器

    php 图片 中文验证码 <img src="verify_image.php" alt="点此刷新验证码" name="verify_code ...

  2. 使用花生壳6.5客户端FTP设置

    1.打开FTP客户端—选项—参数选择 2.设置为主动模式(PORT) 3.连接FTP服务器 4.FTP连接成功

  3. 禁止北京地区IP访问站点

    <script type="text/javascript" src="http://counter.sina.com.cn/ip" charset=&q ...

  4. Linux下动态库(.so)和静态库(.a) 的区别

    静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.编译之后程序文件大,但加载快,隔离性也好.动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还 ...

  5. document获取节点byId&byName

    <script type="text/javascript"> /* *需要:获取页面中的DIV节点: *思路: *通过docment对象完成.因为div节点有ID属性 ...

  6. windows 精简/封装/部署

    给一个精简过的Windows7安装net35,提示自己到『打开或关闭Windows功能』里打开,然而发现并没有,只有一个ie9的功能.搜索尝试各种办法,显然都不行.用dism部署功能的工具,挂载一个完 ...

  7. UrlRewriteFilter

    UrlRewriteFilter是一个改写URL的Java Web过滤器,可见将动态URL静态化.适用于任何Java Web服务器(Resin,Jetty,JBoss,Tomcat,Orion等).与 ...

  8. Interface => IDataErrorInfo

    Introduction to common Interfaces IDataErrorInfo Provides the functionality to offer custom error in ...

  9. iostat监控磁盘io

    1.安装#yum install sysstat 2.启动#/etc/init.d/sysstat start 3.自启动#checkfig sysstat 4.基本使用#iostat -k 2每两秒 ...

  10. 追踪app崩溃率、事件响应链、Run Loop、线程和进程、数据表的优化、动画库、Restful架构、SDWebImage的原理

    1.如何追踪app崩溃率,如何解决线上闪退 当 iOS设备上的App应用闪退时,操作系统会生成一个crash日志,保存在设备上.crash日志上有很多有用的信息,比如每个正在执行线程的完整堆栈 跟踪信 ...