当前位置: 萬仟网 > IT编程>数据库>Mysql > LeetCode——Employees Earning More Than Their Managers

LeetCode——Employees Earning More Than Their Managers

2019年10月09日 17:51  | 萬仟网IT编程  | 我要评论
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      |
+----------+

这种单表比较条件,一般都是表内进行join操作.
参照此思路,解题如下所示:

# write your mysql query statement below
select 
    a.name as employee 
from employee a, employee b
where
    a.managerid = b.id
    and a.salary > b.salary; 

运行效率在可以接受的范围,此外语句也较为清晰便于维护.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

◎已有 0 人评论

Copyright © 2019  萬仟网 保留所有权利. 粤ICP备17035492号-1
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com