Sub topic 10
Finding Leaf Rows with CONNECT_BY_ISLEAF
This chapter assumes you know basic SELECT, WHERE, and ORDER BY, understand primary and foreign keys, and have used self-joins. It also assumes that employees.manager_id points to another row's employee_id.
A leaf row is a row with no children in the hierarchy being traversed. In an employee tree, a leaf is an employee who has no direct reports. Oracle provides the CONNECT_BY_ISLEAF pseudocolumn to identify these rows.
How Oracle identifies a leaf
CONNECT_BY_ISLEAF returns:
1when the current row has no child that satisfies theCONNECT BYcondition.0when the current row has at least one such child.
The result depends on the hierarchy definition. An employee can be a leaf in one query and not a leaf in another if the query uses different roots or filtering conditions.
SELECT employee_id,
last_name,
manager_id,
LEVEL AS depth,
CONNECT_BY_ISLEAF AS is_leaf
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;The query starts at employees without managers. It then follows each employee_id to matching child rows whose manager_id contains that value. Oracle calculates CONNECT_BY_ISLEAF after examining the children available at each step.
| `is_leaf` value | Meaning |
|---|---|
| 1 | No matching child exists |
| 0 | At least one matching child exists |
A root employee can also be a leaf. This happens when the table contains only one employee in the selected hierarchy, or when that employee has no reports.
Selecting only leaf rows
You can display the flag directly, but a common requirement is to return only leaves. Put the hierarchical query in an inline view, then filter its result:
SELECT employee_id,
last_name,
manager_id,
depth
FROM (
SELECT employee_id,
last_name,
manager_id,
LEVEL AS depth,
CONNECT_BY_ISLEAF AS is_leaf
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
)
WHERE is_leaf = 1
ORDER BY last_name;The inner query builds the complete hierarchy and marks each row. The outer query keeps only rows marked with 1.
This pattern is useful when you need the names of employees without direct reports, the final folders in a directory tree, or the terminal states in another parent-child structure.
Leaf status belongs to the query
The word “leaf” does not always mean “has no children anywhere in the table.” It means “has no children reachable through this query's hierarchy.”
For example, suppose a CONNECT BY condition includes a salary rule:
SELECT employee_id,
last_name,
CONNECT_BY_ISLEAF AS is_leaf
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
AND salary > 5000;An employee may have a report whose salary is 5000 or less. That report does not satisfy the CONNECT BY condition, so Oracle does not count it as a child for this traversal. The employee can therefore appear as a leaf in this result.
Common mistake
Do not infer leaf status only from a null manager_id. A null manager_id identifies a root, not a leaf. A root can have many descendants, while a leaf usually has a non-null manager_id.
Use CONNECT_BY_ISLEAF when the question is about the end of the traversed hierarchy. Use manager_id IS NULL when the question is about its starting rows.
Exercise
How would you return the last names of employees who have no direct reports in the hierarchy rooted at employee 100?
Show answer
Use an inline view with CONNECT_BY_ISLEAF, start with employee 100, follow PRIOR employee_id = manager_id, and filter the outer query with WHERE is_leaf = 1.