comments | difficulty | edit_url | tags | |
---|---|---|---|---|
true |
简单 |
|
表:Employee
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | salary | int | | managerId | int | +-------------+---------+ id 是该表的主键(具有唯一值的列)。 该表的每一行都表示雇员的ID、姓名、工资和经理的ID。
编写解决方案,找出收入比经理高的员工。
以 任意顺序 返回结果表。
结果格式如下所示。
示例 1:
输入: Employee 表: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | Null | | 4 | Max | 90000 | Null | +----+-------+--------+-----------+ 输出: +----------+ | Employee | +----------+ | Joe | +----------+ 解释: Joe 是唯一挣得比经理多的雇员。
我们可以通过自连接 Employee
表,找出员工的工资以及其经理的工资,然后筛选出工资比经理高的员工。
import pandas as pd
def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
merged = employee.merge(
employee, left_on="managerId", right_on="id", suffixes=("", "_manager")
)
result = merged[merged["salary"] > merged["salary_manager"]][["name"]]
result.columns = ["Employee"]
return result
# Write your MySQL query statement below
SELECT e1.name Employee
FROM
Employee e1
JOIN Employee e2 ON e1.managerId = e2.id
WHERE e1.salary > e2.salary;