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
相关推荐
素材积累40 分钟前
博士后出站来深可申请的项目补贴等
数据库
_1_72 小时前
SQL Server 磁盘满了 收缩日志
数据库·sqlserver
basketball6162 小时前
Redis基础:1. Redis介绍
数据库·redis·缓存
李可以量化3 小时前
成交量的终极量化策略:价量共振指标完整实现(下篇)
前端·数据库·人工智能
汽车仪器仪表相关领域4 小时前
南华 NHAT-610 柴油车排放测试仪 产品详解
数据库·功能测试·汽车·压力测试·可用性测试
我滴老baby5 小时前
工业时序数据实战:基于 DolphinDB 流计算引擎的实现与调优
数据库
睡不醒男孩0308235 小时前
TiDB数据库调研
数据库·tidb
珠***格6 小时前
实操落地|防逆流装置的安装规范、调试标准与故障处置
网络·数据库·人工智能·分布式·能源·边缘计算
Omics Pro6 小时前
3种蛋白结构输入方式!已申报欧洲发明专利
数据库·人工智能·python·机器学习·plotly
itfallrain7 小时前
Spring 构造器循环依赖排查:@RequiredArgsConstructor + @Lazy 到底有没有生效
数据库·python·spring