File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Managers with at Least 5 Direct Reports
3
+
4
+ The Employee table holds all employees including their managers. Every employee has an Id,
5
+ and there is also a column for the manager Id.
6
+
7
+ +------+----------+-----------+----------+
8
+ |Id |Name |Department |ManagerId |
9
+ +------+----------+-----------+----------+
10
+ |101 |John |A |null |
11
+ |102 |Dan |A |101 |
12
+ |103 |James |A |101 |
13
+ |104 |Amy |A |101 |
14
+ |105 |Anne |A |101 |
15
+ |106 |Ron |B |101 |
16
+ +------+----------+-----------+----------+
17
+ Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table,
18
+ your SQL query should return:
19
+
20
+ +-------+
21
+ | Name |
22
+ +-------+
23
+ | John |
24
+ +-------+
25
+ Note:
26
+ No one would report to himself.
27
+ */
28
+
29
+ # MY SOLUTION (Faster)
30
+ SELECT Name
31
+ FROM Employee
32
+ WHERE Id IN (SELECT ManagerId
33
+ FROM Employee
34
+ WHERE ManagerId IS NOT null
35
+ GROUP BY ManagerId
36
+ HAVING COUNT (ManagerId) >= 5 )
37
+
38
+ # MY SOLUTION (Slower)
39
+ SELECT m .Name
40
+ FROM Employee e
41
+ INNER JOIN Employee m
42
+ WHERE e .ManagerId = m .Id
43
+ GROUP BY e .ManagerId
44
+ HAVING COUNT (* ) >= 5
You can’t perform that action at this time.
0 commit comments