2016년 7월 28일 목요일

[IT실무고급교육★탑크리에듀]EMP 테이블에서 부서별 급여상위3명을 출력하세요.[SQL,RANK,상관서브쿼리,inline view]


EMP 테이블에서 부서별 급여상위3명을 출력하세요.[SQL,RANK,상관서브쿼리,inline view] 

1. 자신보다 급여높은 사람이 0, 1, 2명까지 이므로 아래처럼 풀면 된다. 

select * from emp e1 
where 3 > ( select count(sal) from emp e2 
            where e2.sal > e1.sal 
            and  e2.deptno = e1.deptno) 
order by deptno, sal 

2. Rank함수를 이용하여 인라인뷰로... 

select * from ( 
    select empno, ename, sal, deptno, 
          rank() over(partition by deptno order by sal desc) as rank 
    from emp      

where rank <= 3 
order by deptno, rank

댓글 없음:

댓글 쓰기