본문 바로가기
Programming/LeetCode

[LeetCode] Department Highest Salary

by 데이터현 2022. 3. 31.

https://leetcode.com/problems/department-highest-salary/

 

Department Highest Salary - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

부서별 봉급이 가장 높은 사람의 아이디, 봉급, 부서이름을 출력

 

나의 풀이

SELECT 
    D.name AS Department,
    Employee,
    salary 
FROM
    (
        SELECT 
            name AS Employee,
            salary,
            E.departmentId 
        FROM Employee E,
        (
        SELECT
            MAX(salary) AS m,
            departmentId 
        FROM Employee
        GROUP BY departmentId
        )AS top
        WHERE (top.m, top.departmentId) = (E.salary, E.departmentId)
    ) AS MAX_E
JOIN Department D
ON MAX_E.departmentId = D.id
;

 

풀면서 뭔가 더 단축할 수 있을 것 같았다.

 

LeetCode 솔루션

SELECT
    Department.name AS 'Department',
    Employee.name AS 'Employee',
    Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
    )
;

이렇게 푸는게 훨씬 가독성이 좋아보인다.

근데 속도는 내가 더 빨랐다.. 왜지?

'Programming > LeetCode' 카테고리의 다른 글

[LeetCode] Next Permutation  (0) 2022.04.03
[LeetCode] Valid Palindrome II  (0) 2022.04.02
[LeetCode] Customers Who Never Order  (0) 2022.03.31
[LeetCode] Duplicate Emails  (0) 2022.03.31
[LeetCode] Employees Earning More Than Their Managers  (0) 2022.03.31

댓글