Leecode_SQL50_570. Managers with at Least 5 Direct Reports

Leecode

  1. Managers with at Least 5 Direct Reports

Problem description

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

My solution

sql 复制代码
WITH a AS(
    SELECT m.name, COUNT(e.managerId) AS coun
    FROM Employee e
        JOIN Employee m
            ON e.managerId = m.id
    GROUP BY e.managerId
)
SELECT a.name
FROM a
WHERE coun >= 5
相关推荐
cqsztech1 分钟前
oracle linux 9.6上安装oracle database 19.3 标准版 docker
linux·数据库·oracle
遇见火星7 分钟前
MySQL 性能优化
数据库·mysql·性能优化
safestar20128 分钟前
数据到底存在了哪儿?——拆解MySQL的存储引擎与一致性实现
数据库·mysql
kwg12611 分钟前
Dify二次开发-AI 应用端反馈指令接收(AI 应用端 → Dify)
前端·数据库·人工智能
Qinana18 分钟前
当AI为你写SQL,连数据库都开始谈恋爱了
人工智能·python·sql
LucidX22 分钟前
MySQL主从复制与读写分离
数据库·mysql
白羊无名小猪31 分钟前
正则表达式(捕获组)
java·mysql·正则表达式
羑悻的小杀马特32 分钟前
Redis之Set:从无序唯一到智能存储,解锁用户画像/社交/统计全场景应用
数据库·redis·set
San3033 分钟前
从 Mobile First 到 AI First:用 Python 和大模型让数据库“开口说话”
数据库·python·sqlite