5 kyu
Employees and managers: part 2 - reloaded
Description:
You have been provided with a PostgreSQL database that contains an employees
table. The employees
table has the following structure:
id
: an integer column that uniquely identifies each employee.name
: a text column that contains the name of the employee.manager_id
: an integer column that identifies the manager of each employee. Themanager_id
corresponds to anid
in the same employees table. This means that our table has a recursive relationship with itself - an employee can be a manager to other employees. It is nullable: top managers do not have managers above them.
The task is exactly the same as in the part 2, namely:
You are required to create a SQL query that generates a report that includes each employee's id, name, and a column named management_chain that presents the full hierarchy of managers above that employee, from the employee's immediate manager to the top manager, in a single string.
Each manager in the hierarchy should be represented by their name and id in parentheses. The immediate manager should appear first in the management_chain, followed by their manager, and so forth until the top manager.
Each manager in the management_chain should be separated by " -> ".
The query should return the following columns:
`id`: The ID of the employee.
`name`: The name of the employee.
`management_chain`: A text string representing the chain of the employee's managers, starting with their immediate manager and continuing all the way up to the top-level manager. Each manager in the chain should be represented as their name followed by their ID in parentheses, and different managers should be separated by ' -> '. If an employee is a top-level manager, this column should be an empty string, not NULL.
Notes:
* Assume that the hierarchy depth is unknown and could be arbitrarily large.
* Result should be ordered by id in asc order
* Please note that the organization's management hierarchy does not have cyclic relationships. In other words, it is guaranteed that there will be no situations where an employee's chain of managers loops back on itself (e.g., A's manager is B, B's manager is C, and C's manager is A).
But now I want you to not
use recursive CTE's for this same question. Can you do it fully I-TE-RA-TI-VUH?
Good luck!
Desired Output
The desired output should look like this:
id | name | management_chain |
----+-------------------+------------------------------------------|
1 | Odell Cummerata | |
2 | Myriam Jones | Odell Cummerata (1) |
3 | Jarod Walsh | Odell Cummerata (1) -> Myriam Jones (2) |
...
SQL
Databases
Restricted
Similar Kata:
Stats:
Created | Jun 12, 2023 |
Published | Jun 12, 2023 |
Warriors Trained | 277 |
Total Skips | 56 |
Total Code Submissions | 520 |
Total Times Completed | 48 |
SQL Completions | 48 |
Total Stars | 9 |
% of votes with a positive feedback rating | 88% of 13 |
Total "Very Satisfied" Votes | 10 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 6 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |