Skip to content

Commit eb3d573

Browse files
committed
task: #1731
1 parent 0a81f5e commit eb3d573

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Have a good contributing!
7070
- [1683. Invalid Tweets](./leetcode/easy/1683.%20Invalid%20Tweets.sql)
7171
- [1693. Daily Leads and Partners](./leetcode/easy/1693.%20Daily%20Leads%20and%20Partners.sql)
7272
- [1729. Find Followers Count](./leetcode/easy/1729.%20Find%20Followers%20Count.sql)
73+
- [1731. The Number of Employees Which Report to Each Employee](./leetcode/easy/1731.%20The%20Number%20of%20Employees%20Which%20Report%20to%20Each%20Employee.sql)
7374
- [1741. Find Total Time Spent by Each Employee](./leetcode/easy/1741.%20Find%20Total%20Time%20Spent%20by%20Each%20Employee.sql)
7475
- [1789. Primary Department for Each Employee](./leetcode/easy/1789.%20Primary%20Department%20for%20Each%20Employee.sql)
7576
- [1965. Employees With Missing Information](./leetcode/easy/1965.%20Employees%20With%20Missing%20Information.sql)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Question 1731. The Number of Employees Which Report to Each Employee
3+
Link: https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Employees
6+
7+
+-------------+----------+
8+
| Column Name | Type |
9+
+-------------+----------+
10+
| employee_id | int |
11+
| name | varchar |
12+
| reports_to | int |
13+
| age | int |
14+
+-------------+----------+
15+
employee_id is the column with unique values for this table.
16+
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).
17+
18+
19+
For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.
20+
21+
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.
22+
23+
Return the result table ordered by employee_id.
24+
*/
25+
26+
SELECT
27+
e.employee_id,
28+
e.name,
29+
(
30+
SELECT COUNT(1)
31+
FROM Employees AS e1
32+
WHERE e1.reports_to = e.employee_id
33+
) AS reports_count,
34+
(
35+
SELECT ROUND(AVG(e2.age)::numeric)
36+
FROM Employees AS e2
37+
WHERE e2.reports_to = e.employee_id
38+
) AS average_age
39+
FROM Employees AS e
40+
WHERE (
41+
SELECT COUNT(1)
42+
FROM Employees AS e1
43+
WHERE e1.reports_to = e.employee_id
44+
) > 0
45+
ORDER BY e.employee_id ASC

0 commit comments

Comments
 (0)