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;

相关内容

热门资讯

烈焰熊熊,堪比“原子弹”,欧洲... 据美国有线电视新闻网(CNN)7月29日报道,席卷法国西南部的大片野火燃烧得如此猛烈,以至于形成了一...
外交部:对日本熊本县地震遇难者... 据凤凰卫视报道,针对日本熊本县发生强震造成人员伤亡,中国外交部发言人毛宁7月29日在例行记者会上表示...
高铁喧哗被劝阻后叫嚣全家体制内... “女子高铁喧哗被劝阻后叫嚣全家体制内”一事迎来官方通报。据新京报报道,7月29日,辽宁北票市公安局发...
广东专业蓝牙耳机定制厂家推荐,... 在珠三角腹地,广东早已成为全球蓝牙耳机产业链的“心脏地带”。从深圳华强北的声学元件批发,到东莞、惠州...
格雷厄姆葬礼变地缘竞价场!两大... 有些葬礼,送别的是逝者;有些葬礼,演的却是活人最现实的政治博弈。当地时间7月28日,美国老牌鹰派参议...
一道小学数学题难住3名医学博士... 3名不同学校的博士同做一道数学题,谁最厉害?近日,一陕西网友发布的视频引发热议。29日,视频发布者“...
华为全新“小艺任务”亮相:整合... 7月29日,在华为鸿蒙电脑新品技术沟通会上,终端云服务小艺业务部总裁贾永利宣布,小艺将迎来“从对话到...
广西日报特稿《王的猜想》火了,... 澎湃新闻记者 蒋子文 实习生 汤煜炜为应对外界购买纸质版特稿《王的猜想》的热情,《广西日报》宣布紧急...
月租20元内!2026年平价云... 不少手游玩家、账号运营从业者挑选云手机时,首要诉求就是低价够用,想要控制每月支出在20元以内,同时兼...
诺奖得主如何看待王虹境遇?“我... 中村修二(2014年诺贝尔物理学奖得主)【导读】近日王虹与邓煜双双获得2026年菲尔兹奖,成为了首批...