This question uses MySQL 8 0, not on mysql5 It has been tested in version 6 and is not guaranteed to be correct.
Title Source:Employees who exceed the manager’s income
Managers also belong to employees. Each employee has an ID and a list of managers corresponding to the employee
Id。 Check the names of employees who earn more than their managers
create table employee ( id int, name varchar(255), salary int, managerId int ); insert into employee values (1, 'Joe', 70000, 3), (2, 'Henry', 80000, 4), (3, 'Sam', 60000, null), (4, 'Max', 90000, null);
select employee.name from employee left join employee e on employee.managerId = e.id where employee.salary > e.salary;
managerIdIt’s the manager
idAt the same time, the manager is also an employee, that is to say, No
managerIdIt’s an ordinary employee, yes
managerIdIt’s the manager.
employeeSelf connection, connection condition is
employee.managerId = e.id, you can connect ordinary employees with managers.
Then filter out
employee.salary > e.salaryJust your employees.
More problem solving References:https://github.com/astak16/bl…