From 431f5ffabb9cc418f15cfeaf7c695cabdb06f00f Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Tue, 29 Jul 2025 13:39:30 +0300 Subject: [PATCH] task: #3482 CHALLENGE COMPLETED (all SQL freemium tasks have solutions) --- README.md | 1 + .../3482. Analyze Organization Hierarchy.sql | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 leetcode/hard/3482. Analyze Organization Hierarchy.sql diff --git a/README.md b/README.md index 53b748b..04eb3ec 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql) - [3374. First Letter Capitalization II](./leetcode/hard/3374.%20First%20Letter%20Capitalization%20II.sql) - [3451. Find Invalid IP Addresses](./leetcode/hard/3451.%20Find%20Invalid%20IP%20Addresses.sql) + - [3482. Analyze Organization Hierarchy](./leetcode/hard/3482.%20Analyze%20Organization%20Hierarchy.sql) - [3554. Find Category Recommendation Pairs](./leetcode/hard/3554.%20Find%20Category%20Recommendation%20Pairs.sql) ## Contributing diff --git a/leetcode/hard/3482. Analyze Organization Hierarchy.sql b/leetcode/hard/3482. Analyze Organization Hierarchy.sql new file mode 100644 index 0000000..fa7d2d4 --- /dev/null +++ b/leetcode/hard/3482. Analyze Organization Hierarchy.sql @@ -0,0 +1,81 @@ +/* +Question 3482. Analyze Organization Hierarchy +Link: https://leetcode.com/problems/analyze-organization-hierarchy/description/?envType=problem-list-v2&envId=database + +Table: Employees + ++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| employee_id | int | +| employee_name | varchar | +| manager_id | int | +| salary | int | +| department | varchar | ++----------------+----------+ +employee_id is the unique key for this table. +Each row contains information about an employee, including their ID, name, their manager's ID, salary, and department. +manager_id is null for the top-level manager (CEO). +Write a solution to analyze the organizational hierarchy and answer the following: + +Hierarchy Levels: For each employee, determine their level in the organization (CEO is level 1, employees reporting directly to the CEO are level 2, and so on). +Team Size: For each employee who is a manager, count the total number of employees under them (direct and indirect reports). +Salary Budget: For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary). +Return the result table ordered by the result ordered by level in ascending order, then by budget in descending order, and finally by employee_name in ascending order. +*/ + +WITH RECURSIVE cte AS ( + SELECT + e1.employee_id, + e1.employee_name, + e1.manager_id, + e1.salary, + 1 AS rank + FROM Employees AS e1 + WHERE e1.manager_id IS NULL + + UNION ALL + + SELECT + e2.employee_id, + e2.employee_name, + e2.manager_id, + e2.salary, + c.rank + 1 AS rank + FROM Employees AS e2 + INNER JOIN + cte AS c + ON e2.manager_id = c.employee_id +), + +teamsize AS ( + SELECT + e3.employee_id AS manager_id, + e3.employee_id AS member_id, + e3.salary AS member_salary + FROM Employees AS e3 + + UNION ALL + + SELECT + ts.manager_id, + e4.employee_id AS member_id, + e4.salary AS member_salary + FROM Employees AS e4 + JOIN --noqa: AM05 + teamsize AS ts + ON e4.manager_id = ts.member_id +) + +SELECT + c.employee_id, + c.employee_name, + c.rank AS level, --noqa: RF04 + COUNT(*) - 1 AS team_size, + SUM(ts.member_salary) AS budget +FROM cte AS c +INNER JOIN + teamsize AS ts + ON c.employee_id = ts.manager_id +GROUP BY c.employee_id, c.employee_name, c.rank +ORDER BY level ASC, budget DESC, c.employee_name ASC