Problem

Table: Employee

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| department  | varchar |
| managerId   | int     |
+-------------+---------+

id is the primary key (column with unique values) for this table. Each row of this table indicates the name of an employee, their department, and the id of their manager. If managerId is null, then the employee does not have a manager. No employee will be the manager of themself.

Write a solution to find managers with at least five direct reports.

Return the result table in any order.

The result format is in the following example.

Examples

Example 1:

Input: Employee table:

+-----+-------+------------+-----------+
| id  | name  | department | managerId |
+-----+-------+------------+-----------+
| 101 | John  | A          | None      |
| 102 | Dan   | A          | 101       |
| 103 | James | A          | 101       |
| 104 | Amy   | A          | 101       |
| 105 | Anne  | A          | 101       |
| 106 | Ron   | B          | 101       |
+-----+-------+------------+-----------+

Output:

+------+
| name |
+------+
| John |
+------+

Solution

Method 1 - CTE and Join

Code

SQL
WITH m5 AS
(
    select managerId, count(id) as cnt FROM employee
    GROUP BY managerId
    HAVING cnt >= 5
)
SELECT name FROM employee e
JOIN m5 ON m5.managerId = e.id

Method 2 - Self Join

Code

SQL
SELECT e1.name
  FROM Employee e1
      JOIN Employee e2
          ON e1.id = e2.managerId
 GROUP BY e1.id
HAVING count(e2.managerId) >= 5;
Python
import pandas as pd

def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
    managers = employee.groupby(
        'managerId', as_index=False
    ).agg(
        reporting=('id', 'count'),
    ).query(
        '5 <= reporting'
    )['managerId']

    return employee[
        employee['id'].isin(managers)
    ][['name']]