Department Highest Salary(Window함수)
문제
코드
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;