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
相关推荐
zuoerjinshu5 小时前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
NocoBase6 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
Hoshino.417 小时前
基于Linux中的数据库操作——下载与安装(1)
linux·运维·数据库
Oueii9 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
未来龙皇小蓝9 小时前
【MySQL-索引调优】11:Group by相关概念
数据库·mysql·性能优化
2401_831824969 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf10 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
twc82910 小时前
大模型生成 QA Pairs 提升 RAG 应用测试效率的实践
服务器·数据库·人工智能·windows·rag·大模型测试
@我漫长的孤独流浪10 小时前
Python编程核心知识点速览
开发语言·数据库·python
2401_8512729910 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python