다연이네

[days08] any, some 연산자 본문

Oracle

[days08] any, some 연산자

 다연  2020. 11. 9. 19:19
반응형

any, some 연산자
서브쿼리가 반환한 여러 결과 값 중 메인쿼리와 조건식을 사용한 결과가 하나라도 true라면

메인쿼리 조건식을 true로 반환해 주는 연산자

 

각 부서에서 가장 급여를 많이 받는 사람 조회 (any와 some 사용)

select *
from emp
where sal=(select max(sal) from emp group by deptno);
--ORA-01427: single-row subquery returns more than one row
--서브쿼리가 결과값을 여러개 돌리는데 sal은 하나니까 오류
select *
from emp
where sal in (select max(sal) from emp group by deptno);
where sal = some(select max(sal) from emp group by deptno);
where sal = any(select max(sal) from emp group by deptno);

--위의 3가지 where절은 같은 결과를 출력한다.

3가지 where절은 모두 같은 결과

select *
from emp
where sal >= all(select max(sal) from emp group by deptno);
-- king뿐 ( 모두 만족한다는 의미 )

 

아래 코드는 무슨 의미일까 ? 

select *
from emp 
where sal<any(select sal from emp where deptno=30) --2850보다 작은 애 뿌라겠다
order by sal, empno;

emp 전체를 확인해보면 deptno가 30인 사원들 중에서 최대 sal은 2850이다.

따라서 sal이 2850보다 작으면 출력하겠다는 의미이다.

반응형
Comments