Skip to content

Commit 5e868b2

Browse files
authored
Merge pull request #15 from iamAntimPal/Branch-1
today solution sql
2 parents 87115b4 + 8ea64ac commit 5e868b2

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
1731. The Number of Employees Which Report to Each Employee
2+
Solved
3+
Easy
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Employees
9+
10+
+-------------+----------+
11+
| Column Name | Type |
12+
+-------------+----------+
13+
| employee_id | int |
14+
| name | varchar |
15+
| reports_to | int |
16+
| age | int |
17+
+-------------+----------+
18+
employee_id is the column with unique values for this table.
19+
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null).
20+
21+
22+
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
23+
24+
Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.
25+
26+
Return the result table ordered by employee_id.
27+
28+
The result format is in the following example.
29+
30+
31+
32+
Example 1:
33+
34+
Input:
35+
Employees table:
36+
+-------------+---------+------------+-----+
37+
| employee_id | name | reports_to | age |
38+
+-------------+---------+------------+-----+
39+
| 9 | Hercy | null | 43 |
40+
| 6 | Alice | 9 | 41 |
41+
| 4 | Bob | 9 | 36 |
42+
| 2 | Winston | null | 37 |
43+
+-------------+---------+------------+-----+
44+
Output:
45+
+-------------+-------+---------------+-------------+
46+
| employee_id | name | reports_count | average_age |
47+
+-------------+-------+---------------+-------------+
48+
| 9 | Hercy | 2 | 39 |
49+
+-------------+-------+---------------+-------------+
50+
Explanation: Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
51+
Example 2:
52+
53+
Input:
54+
Employees table:
55+
+-------------+---------+------------+-----+
56+
| employee_id | name | reports_to | age |
57+
|-------------|---------|------------|-----|
58+
| 1 | Michael | null | 45 |
59+
| 2 | Alice | 1 | 38 |
60+
| 3 | Bob | 1 | 42 |
61+
| 4 | Charlie | 2 | 34 |
62+
| 5 | David | 2 | 40 |
63+
| 6 | Eve | 3 | 37 |
64+
| 7 | Frank | null | 50 |
65+
| 8 | Grace | null | 48 |
66+
+-------------+---------+------------+-----+
67+
Output:
68+
+-------------+---------+---------------+-------------+
69+
| employee_id | name | reports_count | average_age |
70+
| ----------- | ------- | ------------- | ----------- |
71+
| 1 | Michael | 2 | 40 |
72+
| 2 | Alice | 2 | 37 |
73+
| 3 | Bob | 1 | 37 |
74+
+-------------+---------+---------------+-------------+
75+
76+
77+
# Write your MySQL query statement below
78+
79+
SELECT
80+
Manager.employee_id,
81+
Manager.name,
82+
COUNT(Employee.employee_id) AS reports_count,
83+
ROUND(AVG(Employee.age)) AS average_age
84+
FROM Employees AS Manager
85+
INNER JOIN Employees AS Employee
86+
ON (Employee.reports_to = Manager.employee_id)
87+
GROUP BY 1
88+
ORDER BY 1;

0 commit comments

Comments
 (0)