[MySQL] SELF JOIN

테이블

Employee 테이블

id name salary managerId
1 Joe 70000 3
2 Henry 80000 4
3 Sam 60000
4 Max 90000

문제

  • 자신의 매니저보다 많이 salary를 받는 인원을 뽑으시오

SELECT e1.name AS Employee
FROM Employee e1
JOIN Employee e2 ON e1.managerId = e2.id 
WHERE e1.salary > e2.salary 
;

스키마

Create table If Not Exists Employee (id int, name varchar(255), salary int, managerId int);
Truncate table Employee;
insert into Employee (id, name, salary, managerId) values (1, 'Joe', 70000, 3);
insert into Employee (id, name, salary, managerId) values (2, 'Henry', 80000, 4);
insert into Employee (id, name, salary, managerId) values (3, 'Sam', 60000, NULL);
insert into Employee (id, name, salary, managerId) values (4, 'Max', 90000, NULL);

links

social