Contents

Department Highest Salary(Window함수)

   Apr 2, 2023     1 min read

Department Highest Salary

문제

184-1.jpg
184-2.jpg
184-3.jpg

코드

Window함수 사용

MS SQL Server/Oracle

SELECT MS.DEPARTMENT
     , MS.NAME AS EMPLOYEE
     , MS.SALARY
FROM (
     SELECT EMPLOYEE.NAME
          , EMPLOYEE.SALARY
          , DEPARTMENT.NAME AS DEPARTMENT
          , MAX(SALARY) OVER (PARTITION BY DEPARTMENTID) MAX_SALARY
     FROM EMPLOYEE
     INNER JOIN DEPARTMENT ON EMPLOYEE.DEPARTMENTID = DEPARTMENT.ID
     ) MS
WHERE MS.SALARY = MS.MAX_SALARY

Window함수 미사용

Oracle

SELECT D.NAME AS DEPARTMENT
     , E.NAME AS EMPLOYEE
     , E.SALARY
FROM EMPLOYEE E
    INNER JOIN (
        SELECT DEPARTMENTID
             , MAX(SALARY) AS MAX_SALARY
        FROM EMPLOYEE
        GROUP BY DEPARTMENTID
    ) DH ON E.DEPARTMENTID = DH.DEPARTMENTID
          AND E.SALARY = DH.MAX_SALARY
     INNER JOIN DEPARTMENT D ON D.ID = E.DEPARTMENTID;

수행결과

184result.jpg