Interview Questions/SQL/Manager With at Least 5 Direct Reports

Manager With at Least 5 Direct Reports

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have an Employee table where each employee may have a manager (identified by managerId). A manager is also an employee in the same table. Write a SQL query to find the names of managers who have at least 5 direct reports. Return the result in any order.

Database Schema

Employee

Column NameTypeDescription
idINTPrimary key
nameVARCHAREmployee name
departmentVARCHARDepartment code
managerIdINTID of this employee's manager (nullable)

Example

Employee

idnamedepartmentmanagerId
101JohnANULL
102DanA101
103JamesA101
104AmyA101
105AnneA101
106RonB101

Output

name
John

Explanation:

John (id=101) manages Dan, James, Amy, Anne, and Ron - that is 5 direct reports, so John qualifies.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Employee

idnamedepartmentmanagerId
101JohnANULL
102DanA101
103JamesA101
104AmyA101
105AnneA101
106RonB101

Output

name
John