Sub topic 2
START WITH and CONNECT BY: Your First Hierarchical Query
This chapter assumes you know basic SELECT, WHERE, and ORDER BY, along with primary keys, foreign keys, and self-joins. It also assumes you are familiar with Oracle's DUAL table.
Tree data in a self-referencing table
Hierarchical data has parent-child relationships. An employee reports to a manager, a folder contains subfolders, and a category can contain smaller categories.
A self-referencing table stores both sides of the relationship in one table. The manager_id column points to another row's employee_id.
| employee_id | last_name | manager_id |
|---|---|---|
| 100 | King | NULL |
| 101 | Kochhar | 100 |
| 107 | Greenberg | 103 |
Here, King is a root employee because no manager is stored for that row. Kochhar is a child of King because Kochhar.manager_id equals King.employee_id.
The basic hierarchical query
Oracle's hierarchical query syntax uses two clauses:
| Clause | Purpose |
|---|---|
| `START WITH` | Chooses the rows where traversal begins |
| `CONNECT BY` | Describes how a parent row connects to its child rows |
A top-down employee query starts with rows that have no manager. It then finds employees whose manager_id matches the current employee's employee_id.
SELECT employee_id,
last_name,
manager_id,
LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;START WITH manager_id IS NULL identifies the root rows. If your data has one top-level manager, this starts with that manager. If it has several independent departments, every matching row becomes a root.
CONNECT BY PRIOR employee_id = manager_id connects each current parent to its children. The PRIOR operator refers to the row found in the previous step of the traversal. In this expression:
PRIOR employee_idis the parent employee's ID.manager_idis the child employee's stored manager ID.- The equality means that the child's manager is the current parent.
Oracle repeats this process for each child until no more matching rows exist.
Reading the result
The LEVEL pseudocolumn shows the depth of each row. A root has LEVEL 1. Its direct reports have LEVEL 2, and their reports have LEVEL 3.
The result is returned in hierarchical traversal order. A parent appears before its descendants, which makes the output useful for organization charts and nested categories.
A table can contain multiple roots, and a query can also start from a selected employee instead of from the top. For example, this query displays the chosen employee and everyone below that employee:
SELECT employee_id,
last_name,
LEVEL
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id;This is useful when you need one department's hierarchy rather than the entire organization. The starting row is still LEVEL 1, even if that employee has a manager elsewhere in the table.
Exercise
In a query that starts with employee_id = 107, what does LEVEL equal for employee 107, and what does CONNECT BY PRIOR employee_id = manager_id search for next?
Show answer
Employee 107 is LEVEL 1. Oracle then searches for rows whose manager_id equals 107, treating those rows as direct children.