王廷瑋|數位醫療|智慧醫療: 181. Employees Earning More Than Their Managers WFU

2024年7月11日 星期四

181. Employees Earning More Than Their Managers

181. Employees Earning More Than Their Managers

員工表

+-------------+---------+

| 列名 | 類型 |

+-------------+---------+

| id | int |

| name | varchar |

| salary | int |

| managerId | int |

+-------------+---------+

id 是這個表的主鍵(具有唯一值的列)。 這個表的每一行表示一個員工的 ID、他們的名字、薪水以及他們經理的 ID。
解題說明

撰寫一個解決方案,找出薪水高於他們經理的員工。

可以按任意順序返回結果表。

範例:

輸入: 

員工表:

+----+-------+--------+-----------+

| id | name | salary | managerId |

+----+-------+--------+-----------+

| 1 | Joe | 70000 | 3 |

| 2 | Henry | 80000 | 4 |

| 3 | Sam | 60000 | Null |

| 4 | Max | 90000 | Null |

+----+-------+--------+-----------+

輸出:

+----------+

| Employee |

+----------+

| Joe |

+----------+

解釋: Joe 是唯一一個薪水高於他經理的員工。


MySQL


# Write your MySQL query statement below
SELECT e1.name AS Employee
FROM Employee e1
JOIN Employee e2 ON e1.managerId = e2.id
WHERE e1.salary > e2.salary;

410ms


Pandas


import pandas as pd

def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
# Perform a self-join on the employee DataFrame
merged_df = employee.merge(employee, left_on='managerId', right_on='id', suffixes=('', '_mgr'))
# Filter employees whose salary is greater than their manager's salary
result_df = merged_df[merged_df['salary'] > merged_df['salary_mgr']]
# Select only the employee name column for the result
result_df = result_df[['name']]
result_df.columns = ['Employee']
return result_df

67.46MB, 515ms


PostgreSQL


-- Write your PostgreSQL query statement below
SELECT e1.name AS Employee
FROM Employee e1
JOIN Employee e2 ON e1.managerId = e2.id
WHERE e1.salary > e2.salary;

0.00MB, 263ms