【sql学习】LeetCode之181. Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+
| Id | Name  | 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 finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

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

自己的代码:

使用的左连接

Runtime: 304 ms, faster than 54.25% of MySQL online submissions for Employees Earning More Than Their Managers.

SELECT
	t1.Name Employee
FROM
	Employee t1
LEFT JOIN Employee t2 ON t1.ManagerId = t2.Id
WHERE
	t1.Salary > t2.Salary

别人的代码

Runtime: 312 ms, faster than 37.36% of MySQL online submissions for Employees Earning More Than Their Managers.

select e1.Name Employee  
    from Employee as e1 
    inner join Employee as e2 
    on e1.ManagerId = e2.Id and e1.Salary > e2.Salary;


select E1.Name 
from Employee as E1, Employee as E2 
where E1.ManagerId = E2.Id and E1.Salary > E2.Salary

1)这两段代码,效率完全一样

猜你喜欢

转载自blog.csdn.net/iNiBuBian/article/details/88431646