728x90
Table: Employee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| salary | int |
| managerId | int |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.
자기 매니저보다 많이 번 직원의 이름을 순서와 무관하게 리턴하라.
Example 1:
Input:
Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | Null |
| 4 | Max | 90000 | Null |
+----+-------+--------+-----------+
Output:
+----------+
| Employee |
+----------+
| Joe |
+----------+
Explanation: Joe is the only employee who earns more than his manager.
Joe의 매니저는 3번 매니저로, Sam이다. Sam은 60000을, Joe는 70000를 벌었으므로, Joe는 매니저보다 많이 번 직원이다. Henry의 매니저는 4번 매니저로, Max이다. Max는 90000을, Henry는 80000을 벌었으므로, Henry는 매니저보다 많이 번 직원이 아니다.
한편, Sam, Max는 managerId가 Null로, 따로 상위의 매니저가 없는 상태이므로 고려 대상에서 제외된다.
따라서 정답 코드는 아래와 같게 된다.
Code:
SELECT e.name as Employee
FROM Employee m, Employee e
WHERE e.managerId = m.id AND e.salary > m.salary;
where절에서 직원과 매니저아이디를 매칭시키고, 급여를 비교하면 된다.
728x90
'SQL' 카테고리의 다른 글
[Leetcode/SQL] 185. Department Top Three Salaries (0) | 2022.08.21 |
---|---|
[Leetcode/SQL] 596. Classes More Than 5 Students (0) | 2022.08.20 |
[Leetcode/SQL] 262. Trips and Users (0) | 2022.08.18 |
[Leetcode/SQL] 184. Department Highest Salary (0) | 2022.08.17 |
[Leetcode/SQL] 180. Consecutive Numbers (0) | 2022.08.15 |