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
相关推荐
一 乐4 分钟前
健康管理|基于springboot + vue健康管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·学习
学编程就要猛7 分钟前
MySQL:CRUD
数据库·sql·mysql
IT技术分享社区7 分钟前
MySQL实战:自动计算字段如何让查询效率翻倍?
数据库·mysql
Live&&learn37 分钟前
Redis语法入门
数据库·redis
我就是你毛毛哥39 分钟前
Linux 定时备份 MySQL 并推送 Gitee
linux·mysql
未羽出衫44 分钟前
DB-GPT本地模型+tuGragh安装使用
数据库·gpt
忧郁蓝调261 小时前
Redis不停机数据迁移:基于 redis-shake 的跨实例 / 跨集群同步方案
运维·数据库·redis·阿里云·缓存·云原生·paas
VekiSon1 小时前
数据库——基础概念与 SQLite 实践
数据库·sqlite
点云SLAM1 小时前
Boost中Graph模块中boost::edge_capacity和boost::edge_capacity_t
数据库·算法·edge·图论·最大团·最大流算法·boost库使用
五阿哥永琪1 小时前
Redis的常用数据结构
数据结构·数据库·redis