Sub topic 14
CONNECT BY Compared with Recursive WITH
Assume you know basic SELECT, WHERE, and ORDER BY, self-joins, primary and foreign keys, and Oracle's DUAL table. This chapter compares Oracle's legacy hierarchical syntax with recursive subquery factoring, often called recursive WITH.
Both approaches walk a parent-child relationship. In the employees table, employee_id identifies an employee and manager_id points to another row's employee_id.
The two-part model of recursive WITH
A recursive WITH query has two parts:
- The anchor member returns the starting rows.
- The recursive member joins new rows to rows already found.
The UNION ALL combines both parts. The recursive member repeats until the join finds no more children.
WITH org (employee_id, last_name, manager_id, lvl) AS (
SELECT employee_id, last_name, manager_id, 1
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.last_name, e.manager_id, o.lvl + 1
FROM employees e
JOIN org o
ON e.manager_id = o.employee_id
)
SELECT employee_id, last_name, lvl
FROM org
ORDER BY lvl, employee_id;The first SELECT finds top-level employees. The second SELECT finds employees whose manager is in the previous result. The lvl column is ordinary query data that you calculate yourself. It is not a built-in pseudocolumn like LEVEL.
Choosing between the two forms
CONNECT BY is concise and has features designed specifically for trees. LEVEL, START WITH, CONNECT_BY_ROOT, CONNECT_BY_ISLEAF, and SYS_CONNECT_BY_PATH make common hierarchy reports easy to write.
Recursive WITH follows the SQL standard more closely. It makes each step explicit and can carry additional state, such as a calculated total, a path string, or a visited-node list. It is often easier to adapt when the recursion needs more than a simple parent-child walk.
| Need | CONNECT BY | Recursive WITH |
|---|---|---|
| Basic tree traversal | Concise | More verbose |
| Depth value | Built-in `LEVEL` | Carry a calculated column |
| Root and leaf information | Built-in operators | Carry or calculate values |
| Custom recursive state | Less flexible | More flexible |
| Sibling ordering | `ORDER SIBLINGS BY` | Build an ordering key |
| Cycle handling | `NOCYCLE` and `CONNECT_BY_ISCYCLE` | Use Oracle's recursive cycle features or prevent cycles in the data |
Common errors and trade-offs
Do not assume that a recursive WITH query automatically preserves display order. An outer ORDER BY lvl groups rows by depth, but it does not produce a parent-followed-by-children tree. To reproduce that layout, construct a sortable path or ordering key during recursion.
A recursive query also needs a clear stopping condition. If the data contains a cycle, recursion can fail or continue until Oracle detects the problem. CONNECT BY NOCYCLE prevents an error and exposes cycle information through CONNECT_BY_ISCYCLE. When using recursive WITH, review Oracle's cycle-detection syntax for your database version, and enforce valid parent references when possible.
Both forms benefit from an index on employees(manager_id), because each step searches for children by their manager. An index on employee_id is normally already provided by the primary key.
Exercise
In the recursive WITH query, which part finds the roots, and which part finds their children?
Show answer
The anchor member, the first SELECT before UNION ALL, finds roots. The recursive member, the second SELECT, joins each employee's managerid to a previously found employeeid.