> For the complete documentation index, see [llms.txt](https://lei-d.gitbook.io/sql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lei-d.gitbook.io/sql/leetcode/570managers-with-at-least-5-direct-reports.md).

# 570\_Managers with at Least 5 Direct Reports

\[medium]

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       |Department |ManagerId |
+------+----------+-----------+----------+
|101   |John       |A           |null      |
|102   |Dan        |A           |101       |
|103   |James      |A           |101       |
|104   |Amy        |A           |101       |
|105   |Anne       |A           |101       |
|106   |Ron        |B           |101       |
+------+----------+-----------+----------+
```

Given the `Employee` table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:

```
+-------+
| Name  |
+-------+
| John  |
+-------+
```

## Solution 1: using join

beats 33%

```php
SELECT e2.Name
FROM Employee e1 
    JOIN Employee e2 ON e1.ManagerId = e2.Id
GROUP BY e1.ManagerId
HAVING count(e1.Id) >= 5;
```

## Solution 2: using subquery

beats 69%

```php
SELECT Name FROM Employee 
WHERE Id IN 
(SELECT ManagerId FROM Employee
GROUP BY ManagerId
HAVING count(Id) >= 5);
```
