Oracle 高级子查询

  高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据。

1,多列子查询

主查询与子查询返回的多个列进行比较

查询与141号或174号员工的manager_id和department_id相同的其他员工的employee_id, manager_id, department_id

select employee_id, manager_id, department_id

from employees

where (manager_id,department_id) in ( --由于内查询返回两列,所以此处也必须使用两列来进行对应,并且列的顺序一致

select manager_id,department_id --内查询返回2列

from employees

where employee_id in (141,174)

)

and employee_id not in (141,174);

2,FROM子句中使用子查询

将子查询的结果集,作为一个虚表,然后从这个虚表中查询数据行

返回比本部门平均工资高的员工的last_name, department_id, salary及平均工资

select last_name, e.department_id, salary,avgsal

from employees e,(select department_id,round(avg(salary)) avgsal

from employees

group by department_id) avg_sal

where e.department_id = avg_sal.department_id

and e.salary > avg_sal.avgsal

order by e.department_id;

3,单列子查询的其他应用

在主查询的select列表中使用

问题:显式员工的employee_id,last_name和location。其中,若员工department_id与location_id为1800的department_id相同,

则location为’Canada’,其余则为’USA’。

select employee_id,last_name,department_id,

(case when department_id = (select department_id

from departments

where location_id = 1800

)

then 'USA'

else 'Canada' end) location

from employees

order by department_id;

select employee_id,last_name,department_id,

(case department_id when (select department_id

from departments

where location_id = 1800

)

then 'USA'

else 'Canada' end) location

from employees

order by department_id;

select employee_id,last_name,department_id,

decode(department_id,(select department_id

from departments

where location_id = 1800

),'USA',

'Canada') location

from employees

order by department_id;

4,相关子查询,

子查询语句中使用了主查询语句中的表中的数据(这个数据不一定在主查询的select语句中)

相关子查询按照一行接一行的顺序执行,即子查询取主查询表中的每一行数据值,主查询的每一行都执行一次子查询。

使用相关子查询,要考虑是否必须使用、是否合理,否则在不需要使用相关子查询的情况下就能轻易得到查询结果,使用相关子查询反而会使查询的效率下降。

用法:

get:从主查询所用的表中获取候选列

execute:子查询使用主查询的数据,并进行相关的筛选

use:如果满足内查询的条件则返回值

例1:查询员工的employee_id,last_name,要求按照员工的department_name排序

select employee_id,last_name,e.department_id,department_name

from employees e,departments dd

where e.department_id = dd.department_id(+) --该查询返回employees表中的所有行,没返回一行,都会取一个employee_id到子查询中

order by (select department_name

from departments d

where e.department_id = d.department_id); --子查询根据主查询返回的employee_id,在departments表中进行查找,有则返回数据。

desc;

例2:查询员工中工资大于本部门平均工资的员工的last_name,salary,department_id和本部门的平均工资

select last_name,salary,e1.department_id,ss.avgsal

from employees e1,(select department_id,round(avg(salary)) avgsal

from employees

group by department_id) ss --该from字句同前例

where e1.department_id = ss.department_id

and salary > (

select round(avg(salary))

from employees e2

where e1.department_id = e2.department_id --将外查询中的department_id,查找该处的department_id,并将所有的返回结果分组

group by e2.department_id)

order by e1.department_id;

例3:若employees表中employee_id与job_history表中employee_id相同且job_history表中employee_id的数目不小于2,

输出employees中这些相同id的员工的employee_id,last_name和其job_id

select e1.employee_id ,last_name,e1.job_id

from employees e1

where (select count(job_id)

from job_history j1

where e1.employee_id = j1.employee_id) >= 2

5,exists

EXISTS 操作符检查在子查询中是否存在满足条件的行

如果在子查询中存在满足条件的行:

不在子查询中继续查找

条件返回 TRUE

如果在子查询中不存在满足条件的行:

条件返回 FALSE

继续在子查询中查找

查询公司管理者的employee_id,last_name,job_id,department_id信息

子查询

select employee_id,last_name,job_id,department_id

from employees e1

where e1.employee_id in (

select distinct(manager_id)

from employees);

自连接

select distinct e1.employee_id,e1.last_name,e1.job_id,e1.department_id

from employees e1,employees e2

where e1.employee_id = e2.manager_id;

相关子查询

select employee_id,last_name,job_id,department_id

from employees e1

where e1.employee_id in (

select manager_id

from employees e2

where e1.employee_id = e2.manager_id);

使用exists相关子查询

select employee_id,last_name,job_id,department_id

from employees e1

where exists (

select 'A' --根据e1表中的每个 employee_id在e2中查找 manager_id,如果找到,子查询返回true,主查询返回该数据行

from employees e2

where e1.employee_id = e2.manager_id);

6.not exists

查询departments表中,不存在于employees表中的部门的department_id和department_name

select d.department_id,d.department_name

from departments d

where not exists ( --not exists 返回子查询中false的结果

select 'X' --根据e1表中的每个 employee_id在e2中查找 manager_id,如果找到,子查询返回true,即该部门在departments中存在,也有员工

from employees e

where d.department_id = e.department_id);

7.相关更新

使用相关子查询依据一个表中的数据更新另一个表的数据

例:向employees中添加一列 department_name ,并更具department_id填充

update employees e1

set department_name = (

select department_name

from departments

where e1.department_id = department_id);

8,相关删除

使用相关子查询依据一个表中的数据删除另一个表的数据

例:删除表employees中,其与emp_history表皆有的数据

delete from employees e1

where employee_id in (

select employee_id

from job_history

where e1.employee_id = employee_id);

9,使用 WITH 子句

可以避免在 SELECT 语句中重复书写相同的语句块

WITH 子句将该子句中的语句块执行一次并存储到用户的临时表空间中

使用 WITH 子句可以提高查询效率

例,查询公司中各部门的总工资,大于公司中各部门的平均总工资,的部门信息

使用普通方法:使用普通方法,汇总后的语句冗长不易理解

--各部门总工资

select department_id,sum(salary) sum_sal from employees group by department_id;

--各部门的平均总工资

select sum(to_sal.sum_sal)/count(*) to_avg_sal

from (select department_id,sum(salary) sum_sal

   from employees group by department_id) to_sal;

--根据前面两项,查询最终结果

select to_sal.department_id,sum_sal

from (select department_id,sum(salary) sum_sal from employees group by department_id) to_sal,

   (select sum(to_sal.sum_sal)/count(*) to_avg_sal from (select department_id,sum(salary) sum_sal from employees group by department_id) to_sal) avg_sal

where to_sal.sum_sal > avg_sal.to_avg_sal;

使用with字句

with to_sal as --各部门总工资

(select department_id,sum(salary) sum_sal from employees group by department_id),

avg_sal as --各部门的平均总工资

(select sum(to_sal.sum_sal)/count(*) to_avg_sal from to_sal)

select department_id,sum_sal

from to_sal

where to_sal.sum_sal > (

select avg_sal.to_avg_sal

from avg_sal);

with

to_sal as

(select department_id,sum(salary) sum_sal from employees group by department_id),

avg_sal as

(select sum(to_sal.sum_sal)/count(*) to_avg_sal from to_sal)

select department_id,sum_sal,to_avg_sal

from to_sal,avg_sal

where to_sal.sum_sal > avg_sal.to_avg_sal;

注明:本博文系学习尚硅谷网易云课堂教学课程总结输出。

Oracle子查询之高级子查询的更多相关文章

  1. Oracle学习DayFour(高级子查询)

    一.高级子查询 1.多列子查询 定义:主查询与子查询返回的多个列进行比较 多列子查询中的比较分为两种:成对比较:不成对比较 实例:查询与141号或174号员工的manager_id和departmen ...

  2. Oracle 学习笔记 14 -- 集合操作和高级子查询

    Oracel提供了三种类型的集合操作:各自是并(UNION) .交(INTERSECT). 差(MINUS) UNION :将多个操作的结果合并到一个查询结果中,返回查询结果的并集,自己主动去掉反复的 ...

  3. Oracle入门第六天(下)——高级子查询

    一.概述 主要内容: 二.子查询介绍 1.简单子查询(WHERE子查询) SELECT last_name FROM employees WHERE salary > (SELECT salar ...

  4. Oracle系列八 高级子查询

    子查询 子查询 (内查询) 在主查询执行之前执行 主查询(外查询)使用子查询的结果 多列子查询 主查询与子查询返回的多个列进行比较 多列子查询中的比较分为两种: 成对比较 问题:查询与141号或174 ...

  5. Oracle-3 - :超级适合初学者的入门级笔记--用户权限,set运算符,高级子查询

    上一篇的内容在这里第二篇内容, 用户权限:创建用户,创建角色,使用grant  和 revoke 语句赋予和回收权限,创建数据库联接 创建用户:create user xxx identified b ...

  6. Oracle子查询之简单子查询

    Oracle 简单子查询 顾名思义,简单子查询是嵌套在 SQL 语句中的另一个SELECT 语句,并且子查询只返回一列数据 1,单行子查询: 子查询 (内查询) 在主查询之前一次执行完成.子查询的结果 ...

  7. ORACLE 多表连接与子查询

    Oracle表连接 SQL/Oracle使用表连接从多个表中查询数据 语法格式: select 字段列表from table1,table2where table1.column1=table2.co ...

  8. Oracle子查询和多表查询

    多表查询需要用到表的连接 连接可以分为:(自行百度) 交叉连接(数字逻辑的笛卡尔积,不做解释) 等值连接 例如:select * from t_a, t_b where t_a.xx = t_b.xx ...

  9. Oracle 数据库基础学习 (六) 子查询

    子查询在一个select中出现多个嵌套查询语句 1.在where子句中使用子查询(一般返回"单行单列" "单行多列" "多行单列"(可以提供 ...

随机推荐

  1. java TreeSet 实现存自定义不可重复数据

    本文主要是介绍一下java集合中的比较重要的Set接口下的可实现类TreeSet TreeSet类,底层用二叉树的数据结构 * 集合中以有序的方式插入和抽取元素. * 添加到TreeSet中的元素必须 ...

  2. ECMAScript5提供了9个新数组方法:遍历、映射、过滤、检测、简化、和搜索数组

    大多数方法的第一个参数接收一个函数,并且对数组的每个元素调用一次该函数.如果是稀疏数组,对不存在的元素不调用传递的函数.在大多数情况下,调用提供的函数使用三个参数:数组元素,元素的索引,数组本身,通常 ...

  3. [WC2016]挑战NPC

    Sol 这做法我是想不到\(TAT\) 每个筐子拆成三个相互连边 球向三个筐子连边 然后跑一般图最大匹配 这三个筐子间最多有一个匹配 那么显然每个球一定会放在一个筐子里,一定有一个匹配 如果筐子间有匹 ...

  4. 手贱--npm 误改全局安装路径

    修改全局安装命令: 通过 npm config set prefix "目录路径" 来设置. 通过 npm config get prefix 来获取当前设置的目录. 我的node ...

  5. arm汇编学习(四)

    一.android jni实现1.静态实现jni:先由Java得到本地方法的声明,然后再通过JNI实现该声明方法.2.动态实现jni:先通过JNI重载JNI_OnLoad()实现本地方法,然后直接在J ...

  6. C# 中重载自增自减操作符的具体运算原理 ----从C++程序员的角度看C#自增操作符重载的实质

    看了看C#的运算符重载,发现与C++打不相同.刚刚被C#的自增操作符坑了,现在来分享一下. 先定义一个类 class A { public int i; public A(int I) { i = I ...

  7. HTML 折行br

    HTML 折行 如果您希望在不产生一个新段落的情况下进行换行(新行),请使用 <br /> 标签: <p>This is<br />a para<br /&g ...

  8. MySQL5.7的组提交与并行复制

    从MySQL5.5版本以后,开始引入并行复制的机制,是MySQL的一个非常重要的特性. MySQL5.6开始支持以schema为维度的并行复制,即如果binlog row event操作的是不同的sc ...

  9. mssql删除数据库、删除帐号错误解决方法

    1. 删除数据库或者恢复数据库时,一定要先将数据库离线,在执行删除.恢复操作. SQL代码如下: /*使数据库离线*/ ALTER DATABASE [数据库名] SET OFFLINE WITH R ...

  10. MyCAT详解

    一.MyCAT概述 MyCAT是一款由阿里Cobar演变而来的用于支持数据库读写分离.分片的分布式中间件.MyCAT可不但支持Oracle.MSSQL.MYSQL.PG.DB2关系型数据库,同时也支持 ...