Skip to content

Commit 5496179

Browse files
committed
prblem no 570 in SQL i Solved
1 parent 78f34ad commit 5496179

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
570. Managers with at Least 5 Direct Reports
2+
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
3+
SQL Schema
4+
Pandas Schema
5+
Table: Employee
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| id | int |
11+
| name | varchar |
12+
| department | varchar |
13+
| managerId | int |
14+
+-------------+---------+
15+
id is the primary key (column with unique values) for this table.
16+
Each row of this table indicates the name of an employee, their department, and the id of their manager.
17+
If managerId is null, then the employee does not have a manager.
18+
No employee will be the manager of themself.
19+
20+
21+
Write a solution to find managers with at least five direct reports.
22+
23+
Return the result table in any order.
24+
25+
The result format is in the following example.
26+
27+
28+
29+
Example 1:
30+
31+
Input:
32+
Employee table:
33+
+-----+-------+------------+-----------+
34+
| id | name | department | managerId |
35+
+-----+-------+------------+-----------+
36+
| 101 | John | A | null |
37+
| 102 | Dan | A | 101 |
38+
| 103 | James | A | 101 |
39+
| 104 | Amy | A | 101 |
40+
| 105 | Anne | A | 101 |
41+
| 106 | Ron | B | 101 |
42+
+-----+-------+------------+-----------+
43+
Output:
44+
+------+
45+
| name |
46+
+------+
47+
| John |
48+
+------+
49+
50+
Explanation:
51+
John is the only manager who has at least 5 direct reports.
52+
53+
Example 2:
54+
55+
Input:
56+
Employee table:
57+
+-----+-------+------------+-----------+
58+
| id | name | department | managerId |
59+
+-----+-------+------------+-----------+
60+
| 101 | John | A | null |
61+
| 102 | Dan | A | 101 |
62+
| 103 | James | A | 101 |
63+
| 104 | Amy | A | 101 |
64+
65+
+-----+-------+------------+-----------+
66+
Output:
67+
+-----------+
68+
69+
# Write your MySQL query statement below
70+
71+
-- Solution 2: Using subquery for counting direct reports and join for filtering managers with at least 5 direct reports
72+
SELECT name
73+
FROM Employee
74+
WHERE id IN (
75+
SELECT managerId
76+
FROM Employee
77+
GROUP BY managerId
78+
HAVING COUNT(*) >= 5
79+
);
80+
81+
-- Solution 3: Using window function for counting direct reports and filtering managers with at least 5 direct reports
82+
83+
SELECT name
84+
FROM Employee
85+
JOIN (
86+
SELECT managerId
87+
FROM Employee
88+
GROUP BY managerId
89+
HAVING COUNT(*) >= 5
90+
) AS managers
91+
ON Employee.id = managers.managerId
92+
93+
GROUP BY name
94+
HAVING COUNT(*) >= 5;

0 commit comments

Comments
 (0)