Skip to main content

LeetCode 570. Managers with at Least 5 Direct Report SQL Solution

Problem

LeetCode SQL Problem

  1. Managers with at Least 5 Direct Report

Employee table

IdNameDepartmentManagerId
101JohnA
102DanA101
103JamesA101
104AmyA101
105AnneA101
106RonB101

Solution

Managers with at Least 5 Direct Report
WITH MgrWithAtLeastFiveDirectReport
AS (
SELECT M.ManagerId
FROM Employee AS M
GROUP BY M.ManagerId
HAVING count(M.Id) >= 5
)
SELECT E.Name
FROM MgrWithAtLeastFiveDirectReport AS M
INNER JOIN Employee AS E ON M.ManagerId = E.Id

Query Output

Name
John