Algorithm/SQL

[SQL][Leetcode] 184. Department Highest Salary

융무 2020. 6. 21. 23:10

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

Problem                                                                                                                      

Analysis                                                                                                                     

1. Employee의 DepartmentId 와 Department의 Id가 같은 곳을 JOIN한다.

2. 그중에 MAX함수를 사용하여 Salary가 높은 곳을 찾는다.

 

Solution                                                                                                                    

SELECT D.NAME AS Department,E.Name AS Employee, E.Salary   
FROM Employee AS E
JOIN Department AS D ON E.DepartmentId = D.Id 
    AND E.Salary = (
        SELECT MAX(Salary) 
        FROM Employee AS T
        WHERE T.DepartmentId = E.DepartmentId
    );