문제) 입사가 가장 빠른 5명?
select *
from (select *
from emp
order by hiredate)
where rownum <= 5;
문제) 입사가 가장 빠른 5명 이후의 입사자들
select *
from (select *
from emp
order by hiredate)
where rownum > 5;
--> 엉터리
select rownum, e.*
from emp e
order by hiredate;
↓ ↓ ↓
select rownum, e.*
from (select *
from emp
order by hiredate) e;
↓ ↓ ↓
select *
from (select rownum as no, e.*
from (select *
from emp
order by hiredate) e
)
where no > 5;
문제) employees 테이블의 부서 중에서 평균 급여가 가장 적은 부서는?
방법 1) subquery + having
select min(avg(salary))
from employees
group by department_id;
↓ ↓
select department_id, avg(salary)
from employees
group by department_id
having avg(salary) = (select min(avg(salary))
from employees
group by department_id);
방법 2) subquery + rownum
select *
from (select department_id, avg(salary)
from employees
group by department_id
order by 2)
where rownum = 1;
'Oracle > SQL Fundamentals I' 카테고리의 다른 글
10일차 # 7-17: Guidelines (0) | 2012.04.17 |
---|---|
10일차 7 집합연산자 Set Operators(using the set operators) (0) | 2012.04.17 |
9일차 # Top-N reporting(analysis) (0) | 2012.04.16 |
9일차 # Pseudocolumn (0) | 2012.04.16 |
9일차 # 6-16: ANY, ALL (0) | 2012.04.16 |