0% found this document useful (0 votes)
5 views

Hierarchy SQL Problem Script-1

The document outlines the creation and population of an Employee_Hierarchy table in a database, including employee IDs and their reporting structure. It demonstrates a recursive Common Table Expression (CTE) to retrieve employees and those working under them, displaying the hierarchy. The document also includes SQL queries to break down the recursive selection process.

Uploaded by

shanumehta007
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Hierarchy SQL Problem Script-1

The document outlines the creation and population of an Employee_Hierarchy table in a database, including employee IDs and their reporting structure. It demonstrates a recursive Common Table Expression (CTE) to retrieve employees and those working under them, displaying the hierarchy. The document also includes SQL queries to break down the recursive selection process.

Uploaded by

shanumehta007
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

DROP TABLE IF EXISTS Employee_Hierarchy;

CREATE TABLE Employee_Hierarchy


(
Emp_id INTEGER,
Reporting_Id INTEGER
);

INSERT INTO Employee_Hierarchy VALUES (1,null );


INSERT INTO Employee_Hierarchy VALUES (2,1);
INSERT INTO Employee_Hierarchy VALUES (3,1);
INSERT INTO Employee_Hierarchy VALUES (4,2);
INSERT INTO Employee_Hierarchy VALUES (5,2);
INSERT INTO Employee_Hierarchy VALUES (6,3);
INSERT INTO Employee_Hierarchy VALUES (7,3);
INSERT INTO Employee_Hierarchy VALUES (8,4);
INSERT INTO Employee_Hierarchy VALUES (9,4);

select * from Employee_Hierarchy;

-- SOLUTION
with recursive cte as
(select emp_id, emp_id as Employees_working_under_him--, 1 as iteration
from Employee_Hierarchy --where Reporting_Id is null
union all
select cte.emp_id, h.emp_id as Employees_working_under_him--, (iteration+1)
as iteration
from cte
join Employee_Hierarchy h on cte.Employees_working_under_him =
h.Reporting_Id
)
select *
from cte
order by 1,2

-- BREAKUP
select emp_id, emp_id as Employees_working_under_him, 1 as iteration
from Employee_Hierarchy where Reporting_Id is null

select cte.emp_id, h.emp_id as Employees_working_under_him, (iteration+1) as


iteration
from (select emp_id, emp_id as Employees_working_under_him, 1 as iteration
from Employee_Hierarchy where Reporting_Id is null) cte
join Employee_Hierarchy h on cte.emp_id = h.Reporting_Id

select cte.emp_id, h.emp_id as Employees_working_under_him, (iteration+1) as


iteration
from (select cte.emp_id, h.emp_id as Employees_working_under_him, (iteration+1) as
iteration
from (select emp_id, emp_id as Employees_working_under_him, 1 as
iteration
from Employee_Hierarchy where Reporting_Id is null) cte
join Employee_Hierarchy h on cte.emp_id = h.Reporting_Id) cte
join Employee_Hierarchy h on cte.emp_id = h.Reporting_Id

select cte.emp_id, h.emp_id as Employees_working_under_him, (iteration+1) as


iteration
from (select cte.emp_id, h.emp_id as Employees_working_under_him, (iteration+1) as
iteration
from (select emp_id, emp_id as Employees_working_under_him, 1 as
iteration
from Employee_Hierarchy where Reporting_Id is null) cte
join Employee_Hierarchy h on cte.emp_id = h.Reporting_Id) cte
join Employee_Hierarchy h on cte.Employees_working_under_him = h.Reporting_Id

You might also like