-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path570_Managers_with_at_Least_5_Direct_Reports.sql
43 lines (35 loc) · 1.68 KB
/
570_Managers_with_at_Least_5_Direct_Reports.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- Source: https://leetcode.com/problems/managers-with-at-least-5-direct-reports/description/?envType=study-plan-v2&envId=top-sql-50
-- 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.
------------------------------------------------------------------------------
-- SQL Schema
Create table If Not Exists Employee (id int, name varchar(255), department varchar(255), managerId int)
Truncate table Employee
insert into Employee (id, name, department, managerId) values ('101', 'John', 'A', 'None')
insert into Employee (id, name, department, managerId) values ('102', 'Dan', 'A', '101')
insert into Employee (id, name, department, managerId) values ('103', 'James', 'A', '101')
insert into Employee (id, name, department, managerId) values ('104', 'Amy', 'A', '101')
insert into Employee (id, name, department, managerId) values ('105', 'Anne', 'A', '101')
insert into Employee (id, name, department, managerId) values ('106', 'Ron', 'B', '101')
-- MS SQL Server Code
SELECT e.name
FROM (
SELECT e1.id, e1.name
FROM Employee AS e1
JOIN Employee AS e2
ON e1.id = e2.managerId
GROUP BY e1.id, e1.name
HAVING COUNT(DISTINCT e2.id) >= 5
) AS