{ "data": { "question": { "questionId": "3828", "questionFrontendId": "3482", "categoryTitle": "Database", "boundTopicId": 3605805, "title": "Analyze Organization Hierarchy", "titleSlug": "analyze-organization-hierarchy", "content": "
Table: Employees
\n+----------------+---------+\n| Column Name | Type | \n+----------------+---------+\n| employee_id | int |\n| employee_name | varchar |\n| manager_id | int |\n| salary | int |\n| department | varchar |\n+----------------+----------+\nemployee_id is the unique key for this table.\nEach row contains information about an employee, including their ID, name, their manager's ID, salary, and department.\nmanager_id is null for the top-level manager (CEO).\n\n\n
Write a solution to analyze the organizational hierarchy and answer the following:
\n\n1
, employees reporting directly to the CEO are level 2
, and so on).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.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nEmployees table:
\n\n\n+-------------+---------------+------------+--------+-------------+\n| employee_id | employee_name | manager_id | salary | department |\n+-------------+---------------+------------+--------+-------------+\n| 1 | Alice | null | 12000 | Executive |\n| 2 | Bob | 1 | 10000 | Sales |\n| 3 | Charlie | 1 | 10000 | Engineering |\n| 4 | David | 2 | 7500 | Sales |\n| 5 | Eva | 2 | 7500 | Sales |\n| 6 | Frank | 3 | 9000 | Engineering |\n| 7 | Grace | 3 | 8500 | Engineering |\n| 8 | Hank | 4 | 6000 | Sales |\n| 9 | Ivy | 6 | 7000 | Engineering |\n| 10 | Judy | 6 | 7000 | Engineering |\n+-------------+---------------+------------+--------+-------------+\n\n\n
Output:
\n\n\n+-------------+---------------+-------+-----------+--------+\n| employee_id | employee_name | level | team_size | budget |\n+-------------+---------------+-------+-----------+--------+\n| 1 | Alice | 1 | 9 | 84500 |\n| 3 | Charlie | 2 | 4 | 41500 |\n| 2 | Bob | 2 | 3 | 31000 |\n| 6 | Frank | 3 | 2 | 23000 |\n| 4 | David | 3 | 1 | 13500 |\n| 7 | Grace | 3 | 0 | 8500 |\n| 5 | Eva | 3 | 0 | 7500 |\n| 9 | Ivy | 4 | 0 | 7000 |\n| 10 | Judy | 4 | 0 | 7000 |\n| 8 | Hank | 4 | 0 | 6000 |\n+-------------+---------------+-------+-----------+--------+\n\n\n
Explanation:
\n\nNote:
\n\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Employees\":[\"employee_id\",\"employee_name\",\"manager_id\",\"salary\",\"department\"]},\"rows\":{\"Employees\":[[1,\"Alice\",null,12000,\"Executive\"],[2,\"Bob\",1,10000,\"Sales\"],[3,\"Charlie\",1,10000,\"Engineering\"],[4,\"David\",2,7500,\"Sales\"],[5,\"Eva\",2,7500,\"Sales\"],[6,\"Frank\",3,9000,\"Engineering\"],[7,\"Grace\",3,8500,\"Engineering\"],[8,\"Hank\",4,6000,\"Sales\"],[9,\"Ivy\",6,7000,\"Engineering\"],[10,\"Judy\",6,7000,\"Engineering\"]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"