inblog logo
|
stupefyee
    SQL문제풀기

    [SQL 문제 풀이] Managers with at Least 5 Direct Reports (5개 이상의 직접 보고서를 보유한 관리자)

    Stupefyee's avatar
    Stupefyee
    Jun 30, 2025
    [SQL 문제 풀이] Managers with at Least 5 Direct Reports (5개 이상의 직접 보고서를 보유한 관리자)
    Contents
    내가 작성한 쿼리
    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/
    Managers with at Least 5 Direct Reports - LeetCode
    notion image
    최소 다섯 개의 직접 보고서를 가진 관리자를 찾기 위한 해결책을 작성하세요. 결과 테이블을 순서에 상관없이 반환하세요.
     

    내가 작성한 쿼리

    MySQL

    SELECT e1.name FROM Employee e1 JOIN Employee e2 ON e1.id = e2.managerId GROUP BY e1.id HAVING COUNT(*) >= 5

    Oracle

    SELECT e1.name FROM Employee e1 JOIN Employee e2 ON e1.id = e2.managerId GROUP BY e1.id, e1.name HAVING COUNT(*) >= 5

    차이점

    항목
    MySQL
    Oracle
    GROUP BY 대상
    e1.id만 있어도 OK
    SELECT에 있는 모든 비집계 컬럼 필요 (e1.id, e1.name)
     
    Share article
    Contents
    내가 작성한 쿼리

    stupefyee

    RSS·Powered by Inblog