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
相关推荐
codigger6 分钟前
如何保证缓存与数据库一致性
数据库·缓存·程序员
游哇游哇游22 分钟前
Redis 过期删除与内存淘汰源码级详解:惰性+定期删除、近似 LRU/LFU、8 种策略与内存打满排查
数据库·redis·缓存·面试·bootstrap
星落梦生1 小时前
苍穹外卖day05——Redis
数据库·redis·缓存
djjjx.11 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
CoderYanger15 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
MSTcheng.16 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
FungLeo17 小时前
FengCMS · PHP+MySQL 升级实录 · 内容写入一致性:锁等待超时与跨模型子树删除竞态
开发语言·mysql·php·工程实践·fengcms
Yeats_Liao17 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
ZhengEnCi18 小时前
覆盖索引是什么:用借书和外卖看懂回表这件事
sql·mysql·postgresql
鹤落晴春18 小时前
MySQL日志梳理
运维·数据库·mysql