Isabelle
JEN-LI CHEN IN DATA SCIENCE
1 min readJan 15, 2021

--

Leetcode SQL

570. Managers with at Least 5 Direct Reports

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+------+----------+-----------+----------+
|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 |
+------+----------+-----------+----------+

Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:

+-------+
| Name |
+-------+
| John |
+-------+

Note:
No one would report to himself.

Solution:

select Name from Employee
where Id in
(
select ManagerId from Employee
group by 1
having count(*) >= 5
)

Link

--

--

JEN-LI CHEN IN DATA SCIENCE
JEN-LI CHEN IN DATA SCIENCE

Published in JEN-LI CHEN IN DATA SCIENCE

My homepage to record my thought processes for solving SQL and Algorithm questions

Isabelle
Isabelle

Written by Isabelle

In love with telling stories with data