취미가 좋다

181. Employees Earning More Than Their Managers 본문

SQL 문제풀이/Leetcode

181. Employees Earning More Than Their Managers

benlee73 2021. 9. 4. 10:41

https://leetcode.com/problems/employees-earning-more-than-their-managers/

 

Employees Earning More Than Their Managers - 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

Solution

SELECT A.Name as Employee
FROM Employee A
INNER JOIN Employee B ON A.ManagerID = B.ID
WHERE A.Salary > B.Salary
  • INNER JOIN 을 사용해서 Employee 뒤에 매니저의 정보를 Employee 에서 가져다가 붙였다.
  • INNER는 NULL 값에는 동작을 수행하지 않아서 LEFT, RIGHT 보다는 조금 빠르다고 한다.
SELECT Name as Employee
FROM Employee A
WHERE Salary > (SELECT Salary 
		FROM Employee B 
                WHERE A.ManagerID = B.Id)
  • 서브 쿼리를 사용하여 매니저의 급여만 가져와서 비교할 수 있다.
  • 그때그때 다르지만 JOIN을 사용하는 것 보다는 느린 것 같다.

Another

select E1.Name 
from Employee as E1, Employee as E2 
where E1.ManagerId = E2.Id and E1.Salary > E2.Salary
  • 더 빠르거나 효율적인 것은 아니지만 참고하면 좋을 것 같다.
  • 두 테이블을 모두 가져오고, WHERE 문에 AND 를 사용해서 두 조건을 걸어서 해결한다.

'SQL 문제풀이 > Leetcode' 카테고리의 다른 글

178. Rank Scores  (0) 2021.09.06
182. Duplicate Emails  (0) 2021.09.04
177. Nth Highest Salary  (0) 2021.08.13
176. Second Highest Salary  (0) 2021.08.13
175. Combine Two Tables  (0) 2021.08.13
Comments