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
相关推荐
元宇宙时间15 分钟前
美国林氏集团宣布全面进军Web3领域
sql
今心上1 小时前
SQL 查询语句的顺序详解
数据库·sql
code.song1 小时前
教师工作量|基于springBoot的教师工作量管理系统设计与实现(附项目源码+论文+数据库)
数据库·spring boot·后端
Python私教1 小时前
macOS安装Redis教程, 通过brew命令, 时间是2024年9月26日, redis版本是0.7.2
数据库·redis·macos
API199701081102 小时前
深度探索与实战编码:利用Python与AWS签名机制高效接入亚马逊Product Advertising API获取商品详情
数据库·python·aws
Pfirsich Zhang2 小时前
Redis相关知识
数据库·redis·缓存
木鬼与槐2 小时前
MySQL高阶1988-找出没所学校的最低分数要求
android·前端·mysql
AI-入门2 小时前
AI大模型:是走向新的巅峰还是陷入发展的僵局?
数据库·人工智能·缓存·langchain·prompt·agi
SG.xf2 小时前
mysql数据库的基本管理
数据库·mysql
yqssjhf2 小时前
希捷电脑硬盘好恢复数据吗?探讨可能性、方法以及注意事项
大数据·数据库·电脑·希捷电脑硬盘数据恢复