fbpx

Hierarchical Queries

Another Question I came across few days ago. This question tests your understanding of SQL Hierarchical Queries and SELF JOIN. Given the following Employees table:

 

Job Interview Question - Hierarchical Queries

 

Note that this table has a certain hierarchy of employees and managers, such that a certain employee may also be a manager. For example, employee number 7 (Merlin Evans) is also the manager of Elroy Wilson, Charles Thomas, and Rudolph Roberts.

 
First Task – Create a query to display the name of all employees, and the name of their manager.
 

Job Interview Question - Hierarchical Queries

 

Second Task – Create a query to display the hierarchical relationship between a certain employee and his direct and indirect managers.
For example, for employee number 7, the query will display the following result (Merlin reports to Jose, and Jose reports to Alvin):

 

Job Interview Question - Hierarchical Queries

 

For employee number 10, the query will display the following result (Rudolph reports to Merlin, Merlin reports to Jose, and Jose reports to Alvin):

 

Job Interview Question - Hierarchical Queries

 

UpScale Analytics is one of the largest platforms in the world for learning SQL by doing, consisting over 300 SQL exercises at different levels (including solutions), by topics, across over 100 different datasets. More…