In this lesson ,
We need to get the data from table has self foreign key
Using Oracle Free SQL , by select HR schema to connect to all HR tables
in past lessons we get data from one table or many tables joined to each other by foreign key , but now we will get the data from table has self foreign key like EMPLOYEES ,
as you can see employee has manager , this manager is (of course) an employee
in EMPLOYEES table , you can see Foreign key on MANAGER_ID refer to EMPLOYEE_ID
let we need to know the name of employee and his manager as well , we can get this information by query like this 👇
SELECT e.EMPLOYEE_ID , e.FIRST_NAME ||' '|| e.LAST_NAME FULL_NAME ,
m.FIRST_NAME ||' '|| m.LAST_NAME AS "Manager Name"
FROM HR.EMPLOYEES e
LEFT JOIN HR.EMPLOYEES m ON e.MANAGER_ID = m.EMPLOYEE_ID
ORDER BY 1
using same data , we need to list this data in more formal layout (shape) , this is known as Tree Query , Hierarchical Query
and this can be like this 👇
SELECT
LEVEL,
employee_id,
first_name || ' ' || last_name AS employee_name,
manager_id
FROM hr.employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
Explanation of Tree Query , Hierarchical Query
to understand this query , we must know the keys of any Tree Query , Hierarchical Query , we can list them as 👇
LEVEL → hierarchy depthSTART WITH→ from which node of tree will need to start ( here root node is CEO / top manager)CONNECT BY , PRIOR→ what is the relation is the tree , which is parent node, which is child node , PRIOR , means the parent node
level 1 is employee has no manager (top management )
level 3 has many employees under him (employee 101 is manager of 5 employees , and they appear on level 4)
connect by was CONNECT BY PRIOR employee_id = manager_id , which means list employees followed by staff under his managment
connect by was CONNECT BY PRIOR employee_id = manager_id , which means list employees followed by staff under his managment
BUT
if we need to get the tree by each employee and all managers above of him the query will be
SELECT
LEVEL,
employee_id,
first_name || ' ' || last_name AS employee_name,
manager_id
FROM hr.employees
START WITH employee_id = 113
CONNECT BY employee_id =PRIOR manager_id
in this way , we get tree start from certain employee (113) , and list all manager above him until Steven (which has no manager)
this type of query is very important , and the best practice of it is when applying workflow depend on the hierarchical relationship
now think about get all employee under management of employee has id = 114 , take your time ⌚ ⌚ ⌚
if you write it as this 👇 , you are right 👏
SELECT
LEVEL,
employee_id,
first_name || ' ' || last_name AS employee_name,
manager_id
FROM hr.employees
START WITH employee_id = 114
CONNECT BY PRIOR employee_id = manager_id
yes , you did it , as we agreed , when understand the logic of data + the right syntax , you can write the right query in easy way
in the Hierarchical Query , we can use WHERE clause as well , to filter the output data ,
let we want to get the employee in tree format with only salary > 12000 like this 👇
SELECT
LEVEL,
employee_id,
first_name || ' ' || last_name AS employee_name,
manager_id , SALARY
FROM hr.employees
WHERE SALARY > 12000
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
for more information about Hierarchical Query , you can see this page
in this lesson we talk about Tree Query , Hierarchical Query ,
in coming lessons we will talk about WHERE clause with CASE statement in Oracle