Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the table Employees (id serial, name varchar(255), manager_id int); In this table manager is an employee. Assume the CEO has no manager, but every
Consider the table Employees (id serial, name varchar(255), manager_id int); In this table manager is an employee. Assume the CEO has no manager, but every other employee has one manager. You want to write a query that gives all managers of a given employee with id = 5. SELECT e1.id, e1.name, e1.manager_id FROM employees e1 INNER JOIN employees e2 ON e1.id = e2.manager_id AND e2.id = 5 WITH managerchain AS SELECT id, name, manager_id FROM employees WHERE id = 5 UNION ALL SELECT e.id, e.name, e.manager_id FROM employees e INNER JOIN managerchain m ON e.id = m.manager_id) SELECT * FROM managerchain; SELECT id, name, manager_id FROM employees WHERE id = 5 WITH managerchain AS ( SELECT id, name, manager_id FROM employees UNION ALL SELECT e.id, e.name, e.manager_id FROM employees e INNER JOIN managerchain m ON m.id = e.manager_id) SELECT * FROM managerchain WHERE manager_id = 5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started