Sub topic 3
Building paths and finding the root
This chapter assumes you can write SELECT, WHERE and ORDER BY queries, that you know primary and foreign keys, and that you have used the Oracle DUAL table. The examples use an employees table where managerid holds the employeeid of each person's manager.
Building paths and finding the root
SYSCONNECTBYPATH builds a string of values from the root down to the current row. CONNECTBY_ROOT returns a column value from the root of the current branch.
SELECT last_name,
SYS_CONNECT_BY_PATH(last_name, '/') AS path,
CONNECT_BY_ROOT last_name AS top_manager
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;Each row shows its full chain of managers, and top_manager names the root of its branch.
Finding leaf rows with CONNECT_BY_ISLEAF
A leaf is a row with no children in the result. CONNECTBYISLEAF returns 1 for leaves and 0 otherwise.
SELECT last_name, CONNECT_BY_ISLEAF AS is_leaf
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;Handling cycles in the data
A cycle exists when a row is also its own ancestor. Without NOCYCLE, Oracle raises ORA-01436. With NOCYCLE, the query finishes, and CONNECTBYISCYCLE returns 1 for a row whose child is already one of its ancestors.
SELECT employee_id, manager_id, CONNECT_BY_ISCYCLE AS is_cycle
FROM employees
START WITH employee_id = 100
CONNECT BY NOCYCLE PRIOR employee_id = manager_id;Counting and summing over subtrees
Without START WITH, every row acts as a root. Grouping by CONNECTBYROOT then gives one group per employee. Each group holds that employee and all descendants, so subtracting one removes the employee.
SELECT CONNECT_BY_ROOT employee_id AS manager_id,
COUNT(*) - 1 AS total_reports
FROM employees
CONNECT BY PRIOR employee_id = manager_id
GROUP BY CONNECT_BY_ROOT employee_id;Generating rows with CONNECT BY LEVEL
A CONNECT BY condition that does not use PRIOR, applied to DUAL, produces one row per level. This query returns the numbers 1 through 10.
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 10;CONNECT BY compared with recursive WITH
Recursive WITH is the ANSI-standard form, available from Oracle 11g Release 2. It works in other databases with minor changes. CONNECT BY provides LEVEL, CONNECTBYROOT and CONNECTBYISLEAF, which recursive WITH lacks. With recursive WITH, you track depth yourself in a column.
Performance and indexing considerations
Each step of a walk looks up child rows by managerid. Index managerid so these lookups do not scan the table. The primary key on employee_id usually provides the index for the other side of the join. Place pruning conditions inside CONNECT BY when they should remove whole branches.
Exercise
Write a query that returns the last_name of every employee who has no subordinates, using the employees table.
Show answer
SELECT lastname FROM (SELECT lastname, CONNECTBYISLEAF AS isleaf FROM employees START WITH managerid IS NULL CONNECT BY PRIOR employeeid = managerid) WHERE is_leaf = 1;