570. Managers with at Least 5 Direct Reports

Level: Medium; Bloomberg

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.

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:

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.

Approach 2: JOIN.

Last updated