leetcode181. more than the manager of employee income (SQL)

Employee table contains all employees, their managers belong to employees. Each employee has a Id, in addition to a correspondence Id manager of staff.

---- + ------- + -------- + + ----------- +
| Id | the Name | the Salary | ManagerId |
+ ---- + ----------- -------- + + + -------
|. 1 | Joe | 70000 |. 3 |
| 2 | Henry | 80000 |. 4 |
|. 3 | SAM | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+ ---- + ------- + -------- + ----------- +
given the employee table, write a SQL query that you can get the names of managers earning more than their employees. In the above table, Joe is the only one earning more than his manager's staff.

+----------+
| Employee |
+----------+
| Joe      |
+----------+

Ideas:

Since the connection

# Write your MySQL query statement below
select A.Name as 'Employee'
from Employee as A,Employee as B 
where A.ManagerId=B.Id and A.Salary>B.Salary;

Efficiency sub-query contains the main query is relatively slow, it is not recommended.

select a.name as Employee 
from Employee as a 
where a.salary > (select b.salary 
                    from Employee as b 
                    where b.id = a.managerid);

 

Published 552 original articles · won praise 10000 + · views 1.32 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104315225