inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] Managers with at Least 5 Direct Reports

    silver's avatar
    silver
    Oct 20, 2025
    [SQL문제풀기] Managers with at Least 5 Direct Reports
    Contents
    문제MySQL

    문제

    Managers with at Least 5 Direct Reports - LeetCode
    Can you solve this real interview question? Managers with at Least 5 Direct Reports - Table: Employee +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | department | varchar | | managerId | int | +-------------+---------+ id is the primary key (column with unique values) 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 a solution to find managers with at least five direct reports. Return the result table in any order. The result format is in the following example.   Example 1: Input: Employee table: +-----+-------+------------+-----------+ | 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 | +-----+-------+------------+-----------+ Output: +------+ | name | +------+ | John | +------+
    Managers with at Least 5 Direct Reports - LeetCode
    https://leetcode.com/problems/managers-with-at-least-5-direct-reports/description/?envType=study-plan-v2&envId=top-sql-50
    Managers with at Least 5 Direct Reports - LeetCode

    MySQL

    내가 작성한 정답

    : where이 좀 더 빠르다
    select name from Employee where id in (select managerId from Employee group by managerId having count(id) >= 5);
    select b.name from Employee b left join Employee e on b.id = e.managerId group by b.id having count(e.id) >= 5;
    notion image
    notion image
     
    Share article

    silver

    RSS·Powered by Inblog