Oracle-子查询
admin
2023-04-29 13:43:30
0

一、WHERE条件中的子查询

1. 比black工资高的雇员有哪些?

select ename 

from emp

where sal>(select sal from emp where ename='BLAKE');


2. 高于30部门最高工资的雇员有哪些?

select ename,sal 

from emp

where sal>(select max(sal) from emp where deptno=30);


select ename,sal 

from emp

where sal > all (select sal from emp where deptno=10);   --任何


3. 当all后面接子查询的时候

"x = ALL (...)": The value must match all the values in the list to evaluate to TRUE.所有值都要匹配

"x != ALL (...)": The value must not match any values in the list to evaluate to TRUE.至少有一个值不匹配

"x > ALL (...)": The value must be greater than the biggest value in the list to evaluate to TRUE.大于最大的值

"x < ALL (...)": The value must be smaller than the smallest value in the list to evaluate to TRUE.小于最小的值

"x >= ALL (...)": The value must be greater than or equal to the biggest value in the list to evaluate to TRUE.大于等于最大的值

"x <= ALL (...)": The value must be smaller than or equal to the smallest value in the list to evaluate to TRUE.小于等于最小的值


4. 大于10部门最小工资的雇员有哪些?

select ename,sal 

from emp

where sal> (select min(sal) from emp where deptno=10);


select ename,sal 

from emp

where sal > any (select sal from emp where deptno=10);   --any 大于任何一个,那不就是最小的么??,任意一个


5. 当any后面接子查询的时候

"x = ANY (...)": The value must match one or more values in the list to evaluate to TRUE.至少匹配一个值

"x != ANY (...)": The value must not match one or more values in the list to evaluate to TRUE.一个值都不匹配

"x > ANY (...)": The value must be greater than the smallest value in the list to evaluate to TRUE.大于最小值

"x < ANY (...)": The value must be smaller than the biggest value in the list to evaluate to TRUE.小于最大值

"x >= ANY (...)": The value must be greater than or equal to the smallest value in the list to evaluate to TRUE.大于等于最小值

"x <= ANY (...)": The value must be smaller than or equal to the biggest value in the list to evaluate to TRUE.小于等于最大值


6. 工资最高的人是谁?

select ename from emp

where sal=(select max(sal) from emp);


7. 和ALLEN同部门,工资高于MARTIN的雇员有哪些?

select ename from emp

where deptno=(select deptno from emp where ename='ALLEN')

and sal>(select sal from emp where ename='MARTIN');


8. 工作和部门与SMITH相同,工资高于JAMES的雇员有哪些?

select ename from emp

where (job,deptno)=(select job,deptno from emp where ename='SMITH')

and sal>(select sal from emp where ename='JAMES');



二、FROM子句中的子查询

1. 工资高于本部门平均工资的人(拿上游工资的人)有哪些?

    ①求出每个部门的平均工资,把这个作为一张表

    ②使用emp表和平均工资表进行关联,

select ename,sal,avgsal,e.deptno

from emp e,

(select avg(sal) avgsal,deptno 

from emp

group by deptno) b

where e.deptno=b.deptno

and e.sal>b.avgsal;


三、伪列:rownum 

特点:先有结果集在有rownum,是对结果集的一个编号


1. 工资前五名的人?(TOP-N 分析)

    ①先把工资排序

    ②在使用rownum限结果集(为什么不在第一步就使用rownum限定结果集?执行顺序的问题,where要比order by先执行,获取rownum<6的时候还没来得及排序在从emp里面拿出来

select ename,sal

from emp

where sal in

(select sal 

from (select distinct sal from emp order by sal desc)

where rownum<6)

order by sal desc;


3. 工资6~10的人?

    ①先把工资排序

    ②把工资排名在6~10的拿出来,由于不能使用rownum>6 and xxx<10这样,所以要加一步,把rownum变成id列,这样就又构造成一个结果集

    ③把上一个结果集中id为6~10的条目拿出来

    ④和emp关联

select ename,sal from emp 

where sal in 

(select sal from 

(select rownum rn,sal 

from (select distinct sal 

      from emp order by sal desc))

where rn between 6 and 10)

order by sal desc;

相关内容

热门资讯

五角大楼:伊朗战争花费,250... 美国《华尔街日报》5月12日消息,美国防部负责财务的代理副部长朱尔斯·赫斯特表示,据估计,伊朗战事的...
检察机关依法对林景臻涉嫌受贿案... 检察日报全媒体记者5月13日从最高人民检察院获悉,中国银行股份有限公司原党委委员、副行长林景臻涉嫌受...
从“看山看水”到“乐享山水”—... “十五五”首个“五一”假期,文旅市场热力迸发、全线升温。从“看山看水”到“乐享山水”,从品文化到入生...
工作时间减少1%,肥胖率或下降... 澎湃新闻记者 季敬杰合理调整生活与工作的平衡可能会给健康带来意想不到的好处。近日一项研究指出,年度工...
美媒:中美元首会晤或讨论AI护... 据凤凰卫视援引美国媒体报道,美方希望借中美领导人会晤的机会,开启有关人工智能安全与管控的对话,并推动...
卫生间水管布置图尺寸高度 卫生间的水管布置可能比较复杂一些,因为卫生间的管道比较多。比如有热水器的通水管道,有座便器的冲水管道...
电脑尺寸在哪里看 电脑尺寸在哪里看1、可以选择手工测量法来测量电脑尺寸,这时测量一下屏幕对角线,因为显示面积都会小于显...
电脑机箱尺寸标准 常规的机箱一般是立体式,或桌面式,再就是横式。电脑机箱尺寸,目前市面上根据不同的规格有不同的方案。 ...
斯塔默发声:不会辞职 【环球时报驻英国特约记者 纪双城 环球时报特约记者 甄翔 于文】执政党工党在英国地方选举中遭遇“历史...
抽油烟机管道有异味怎么办 抽油烟机是厨房中不可或缺的设备,它能有效地排出油烟,保证室内空气的清新。但是,长时间使用后,抽油烟机...