欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

LeetCode——Employees Earning More Than Their Managers

程序员文章站 2023-08-19 23:33:24
这种单表比较条件,一般都是表内进行 操作. 参照此思路,解题如下所示: 运行效率在可以接受的范围,此外语句也较为清晰便于维护. ......
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; 

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