[leetcode][sql] SelfJoin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Q2. Employees Earning More Than Their Managers
Easy
Topics
premium lock icon
Companies
SQL Schema
Pandas Schema
Table: Employee
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| salary | int |
| managerId | int |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the ID of an employee,
their name, salary, and the ID of their manager.
Write a solution to find the employees who earn more than their managers.
Return the result table in any order.
The result format is in the following example.
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.
같은 테이블을 두 번 JOIN하는 Self JOIN 문제. Employee 테이블을 직원용(e), 매니저용(m) 두 번 사용 직원의 managerId와 매니저의 id를 연결 직원의 salary가 매니저의 salary보다 큰 경우 찾기
1
2
3
4
5
6
7
# Write your MySQL query statement below
SELECT e.name AS Employee # 이름을 Employee라는 컬럼 별칭으로 내보내.
FROM Employee e # 기존 Employee 테이블을 직원용으로 읽어.
JOIN Employee m On e.managerId = m.id # 거기에 관리자용 테이블을 붙여.
WHERE e.salary > m.salary; # 직원의 월급이 관리자 월급보다 클 때만.
#leetcode #sql #selfJoin
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.