oracle数据库之子查询

子查询也叫内部查询在主查询之前执行一次得到结果此结果一般情况下,是用来当做是主查询的条件。

 

-- 在 emp 表中,找出工资比 ALLEN 的高?

-- 先查出 ALLEN 的工资是多少?

select sal from scott.emp where ename = 'ALLEN'; -- 1600

-- 然后再做比较

select * from scott.emp where sal > 1600;

-- 整合

select * from scott.emp where sal >

(select sal from scott.emp where ename = 'ALLEN');

 

-- 当我们做了子查询之后,会出现多行数据

-- 如果想要做多行比较

-- in:与列表中的任何一个值相等,简单理解,只要在指定范围内即可

-- any:与子查询返回的任意一个可能的值进行比较。

-- all:与子查询返回的所有值进行比较。

 

-- in相当于使用 = 号

select * from scott.emp where sal in

(select sal from scott.emp where ename = 'ALLEN');

 

-- 使用子查询方式实现多表查询

select * from

(

select deptno, count(*), sum(sal)

from scott.emp

group by deptno

);

 

 

posted @ 2018-08-08 08:42  一飞要上天  阅读(580)  评论(0编辑  收藏  举报