Sub topic 9
Building Paths and Finding the Root
This chapter assumes you know basic SELECT statements, self-joins, primary and foreign keys, and Oracle's DUAL table. It also assumes an employees table in which manager_id stores the employee_id of an employee's manager. A NULL manager means the employee is at the top of the organization.
A hierarchical query can return more than the current row. Oracle can also show the complete path from the root to that row and identify the root that started the traversal.
Building a path with SYS_CONNECT_BY_PATH
SYS_CONNECT_BY_PATH is an Oracle function that concatenates a column's values along the current path. Its first argument is the column to collect. Its second argument is the separator to place before each value.
SELECT employee_id,
last_name,
LEVEL AS depth,
SYS_CONNECT_BY_PATH(last_name, '/') AS reporting_path
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;For a hierarchy containing King, Kochhar, and Greenberg, the results might include:
| LAST_NAME | DEPTH | REPORTING_PATH |
|---|---|---|
| King | 1 | /King |
| Kochhar | 2 | /King/Kochhar |
| Greenberg | 3 | /King/Kochhar/Greenberg |
The path begins with the root because the query starts at employees whose manager_id is NULL. Each child extends its parent's path.
The return value is a character expression. If names can contain the separator, choose a separator that cannot be confused with data, or use a different representation for display.
Finding the root with CONNECT_BY_ROOT
CONNECT_BY_ROOT returns a column from the root row of the current hierarchy branch. Unlike SYS_CONNECT_BY_PATH, it does not collect every value. It repeats the root's value on every descendant row.
SELECT employee_id,
last_name,
LEVEL AS depth,
CONNECT_BY_ROOT employee_id AS root_employee_id,
CONNECT_BY_ROOT last_name AS top_manager
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;A branch might look like this:
| LAST_NAME | DEPTH | TOP_MANAGER |
|---|---|---|
| King | 1 | King |
| Kochhar | 2 | King |
| Greenberg | 3 | King |
This is useful when one table contains several independent trees. You can group or filter rows by CONNECT_BY_ROOT to analyze each tree separately.
Combining paths and roots
A report can show both the readable route and the root identity.
SELECT last_name,
SYS_CONNECT_BY_PATH(last_name, ' > ') AS path,
CONNECT_BY_ROOT employee_id AS root_id,
CONNECT_BY_ROOT last_name AS root_name
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;The ORDER SIBLINGS BY clause sorts employees who share the same manager without flattening the hierarchy. The path still follows parent-to-child relationships.
Common mistakes
- Put
CONNECT_BY_ROOTbefore the column expression, such asCONNECT_BY_ROOT last_name. - Remember that
SYS_CONNECT_BY_PATHdescribes the current route, not only the immediate manager. - Do not expect
CONNECT_BY_ROOTto find the root by searching the table independently. It reports the row selected bySTART WITH. - Use a top-down condition,
PRIOR employee_id = manager_id, when paths should run from manager to employee.
Exercise
Which expression returns /King/Kochhar/Greenberg for Greenberg, and which expression returns King on that same row?
Show answer
SYS_CONNECT_BY_PATH(last_name, '/') returns /King/Kochhar/Greenberg; CONNECT_BY_ROOT last_name returns King.