# 570. Managers with at Least 5 Direct Reports

### Question:

Table: `Employee`

```
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| department  | varchar |
| managerId   | int     |
+-------------+---------+
id is the primary key column 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.
```

&#x20;

Write an SQL query to report the managers with at least **five direct reports**.

Return the result table in **any order**.

The query result format is in the following example.

**Example 1:**

<pre><code>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       |
+-----+-------+------------+-----------+
<strong>Output:
</strong> 
+------+
| name |
+------+
| John |
+------+
</code></pre>

### My Solution:

Approach 1: First make a table of managerId and counts of direct reports; then select name based on temp table and Employee, with a constraint that counts of direct reports should be >=5.

```sql
select name
from (select managerId, count(managerId) as num
      from Employee
      group by managerId) t, Employee
where Employee.id=t.managerId and t.num>=5
```

Approach 2: JOIN.&#x20;

```sql
SELECT a.name
from employee a 
inner join employee b on a.id=b.managerId 
group by b.managerId having count(b.managerId)>=5
```
