27. High-Report Managers
A companyβs HR system keeps track of employees and their reporting structure. The management wants to identify managers who have at least 5 employees directly reporting to them.
The company maintains an Employee table that stores:
βββββββββββββββ¦βββββββββββ β Column Name βΒ Β TypeΒ Β β β ββββββββββββββ¬βββββββββββ£ βΒ Β Β idΒ Β Β βΒ Β intΒ Β β βββββββββββββββΌβββββββββββ βΒ Β nameΒ Β Β β varcharΒ β βββββββββββββββΌβββββββββββ β departmentΒ β varcharΒ β βββββββββββββββΌβββββββββββ βΒ managerldΒ βΒ Β intΒ Β β βββββββββββββββ©βββββββββββ
- id: A unique identifier (primary key) for each employee.
- name: The name of the employee.
- department: The department in which the employee works.
- managerId: The id of the employeeβs direct manager (NULL if the employee has no manager).
Your task is to find all managers who have 5 or more employees directly reporting to them.
The result should be returned in any order.
The sample output format is in the following example.
Example 1:
Input:
Employee Table
ββββββββββββ¦βββββββββββ¦βββββββββββββ¦ββββββββββββ βΒ Β IdΒ Β Β βΒ Β nameΒ Β β department β managerId β β βββββββββββ¬βββββββββββ¬βββββββββββββ¬ββββββββββββ£ βΒ Β 101Β Β βΒ Emily Β βΒ Β Β AΒ Β Β βΒ Β NullΒ Β β ββββββββββββΌβββββββββββΌβββββββββββββΌββββββββββββ βΒ Β 102Β Β βΒ Β JakΒ Β βΒ Β Β AΒ Β Β βΒ Β 101Β Β β ββββββββββββΌβββββββββββΌβββββββββββββΌββββββββββββ βΒ Β 103Β Β βΒ OliveΒ Β βΒ Β Β AΒ Β Β βΒ Β 101Β Β β ββββββββββββΌβββββββββββΌβββββββββββββΌββββββββββββ βΒ Β 104Β Β βΒ Β EmyΒ Β βΒ Β Β AΒ Β Β βΒ Β 101Β Β β ββββββββββββΌβββββββββββΌβββββββββββββΌββββββββββββ βΒ Β 105Β Β βΒ Β LiamΒ Β βΒ Β Β AΒ Β Β βΒ Β 101Β Β β ββββββββββββΌβββββββββββΌβββββββββββββΌββββββββββββ βΒ Β 106Β Β βΒ Β LieΒ Β βΒ Β Β BΒ Β Β βΒ Β 101Β Β β ββββββββββββ©βββββββββββ©βββββββββββββ©ββββββββββββ
Output:
ββββββββββββ βΒ Β nameΒ Β β β βββββββββββ£ βΒ EmilyΒ Β β ββββββββββββ
Explanation:
- Emily (id = 101) - Has 5 direct reports (102, 103, 104, 105, 106), so he is included.
- Jak (id = 102), Olive (id = 103), Emy (id = 104), Liam (id = 105), Lie (id = 106) - None of them have 5 or more reports, so they are excluded.
Still unsure what the problem is asking ?
Letβs go through a few more examples, step by step, to make it clearer.