3-5장 ANY와 ALL

2016. 3. 1. 11:26

※ ANY와 ALL

ANY와 ALL은 ①서브쿼리와 함께 쓰이거나 ②값들의 집합과 함께 쓰인다. + 비교 연산자와 함께


②값들의 집합

select * from temp

where salary > all (10000000,20000000,30000000,40000000);


그러나 대게는 ①서브쿼리를 사용할 때 쓰이는데, 만약 질의에 필요할 결과값 집합을 미리 알고 있다면, 굳이 ANY나 ALL연산자를 사용해야 할 이유가 없기 때문이다.


예제

<1-1 ANY>

select emp_id, emp_name, salary

from temp

where salary > any (select salary

                      from temp

                     where lev = '과장');


<1-2 MIN을 사용할 경우 -- 연관성 없는 서브쿼리>    

select emp_id, emp_name, salary

from temp

where salary > (select min(salary)

                      from temp

                     where lev = '과장');


<1-3 ANY로 비교하고자 하는 경우, 연관성 있는 서브쿼리 형태도 가능함>


select emp_id, emp_name, salary

from temp a

where exists (select b.salary

                from temp b

               where b.lev = '과장'

                 and a.salary > b.salary);


* 연관성 있는 서브쿼리의 경우 JOIN이 사용되어야 하며, 이 경우 a.salary > b.salary 구문을 통해 NON-EQUI 조인이 사용되었음을 알 수 있다.



<2-1 ALL>

select emp_id, emp_name, salary

from temp

where salary > all (select salary

                      from temp

                     where lev = '과장');


<2-2 ALL -- 연관성 없는 서브쿼리>                     

select emp_id, emp_name, salary

from temp

where salary > (select max(salary)

                      from temp

                     where lev = '과장');

곰돌곰둘 Oracle_DB_Unix admin/(6)오라클 실습 (이채남 저)