In the example below, the SQL statement is querying a table with Employee ID's and their respective Managers. The CONNECT BY clause denotes the parent rows and the child rows of the hierarchy.
SELECT emp_id, l_name, mgr_id
FROM employees
CONNECT BY PRIOR emp_id = mgr_id;
This query produces the result set that defines the relationship between employees and managers.FROM employees
CONNECT BY PRIOR emp_id = mgr_id;
EMP_ID | L_NAME | MGR_ID |
101 | Brown | 100 |
108 | Greenberg | 101 |
109 | Jones | 108 |
SELECT emp_id, l_name, mgr_id, LEVEL
FROM employees
CONNECT BY PRIOR emp_id = mgr_id;
The result set looks like this:FROM employees
CONNECT BY PRIOR emp_id = mgr_id;
EMP_ID | L_NAME | MGR_ID | LEVEL |
101 | Koch | 100 | 1 |
108 | Greenberg | 101 | 2 |
109 | Franks | 108 | 3 |
SELECT l_name, emp_id, mgr_id, LEVEL
FROM employees
START WITH emp_id = 100
CONNECT BY PRIOR emp_id = mgr_id
ORDER SIBLINGS BY l_name;
FROM employees
START WITH emp_id = 100
CONNECT BY PRIOR emp_id = mgr_id
ORDER SIBLINGS BY l_name;
No comments:
Post a Comment