Sub topic 4
Depth in the Tree with `LEVEL`
Assume you know basic SELECT and WHERE clauses, primary and foreign keys, self-joins, and Oracle's DUAL table. This chapter focuses on LEVEL, an Oracle pseudocolumn used in hierarchical queries.
A self-referencing table stores a relationship between rows in the same table. For example, an employees table can use manager_id to point to another employee:
| employee_id | last_name | manager_id |
|---|---|---|
| 100 | King | `NULL` |
| 101 | Kochhar | 100 |
| 102 | De Haan | 100 |
Employee 100 is the root of this part of the tree. Employees 101 and 102 are one level below that root.
How `LEVEL` works
LEVEL reports the depth of the current row during hierarchical traversal:
- A root row has
LEVEL = 1. - A direct child of a root has
LEVEL = 2. - A grandchild has
LEVEL = 3. - The value increases by one for each parent-child step.
Use START WITH to select the root rows. Use CONNECT BY PRIOR to describe how Oracle finds each row's children.
SELECT employee_id,
last_name,
manager_id,
LEVEL AS tree_level
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;START WITH manager_id IS NULL begins with every employee who has no manager. The condition PRIOR employee_id = manager_id then finds employees whose manager_id matches the current employee's employee_id.
The result might look like this:
| employee_id | last_name | manager_id | tree_level |
|---|---|---|---|
| 100 | King | `NULL` | 1 |
| 101 | Kochhar | 100 | 2 |
| 103 | Greenberg | 101 | 3 |
| 102 | De Haan | 100 | 2 |
The exact order depends on the table and the hierarchical query. The important point is that LEVEL describes the path from the selected root, not a permanent property of the employee. If you start at a different row, the same employee can have a different level.
Using `LEVEL` to understand results
Add LEVEL while developing a hierarchical query. It makes the traversal visible and helps reveal an incorrect CONNECT BY condition. A sudden level that is unexpectedly high can indicate a long chain or incorrect data.
A query can also start at one employee instead of all top-level employees:
SELECT employee_id,
last_name,
LEVEL AS distance_from_start
FROM employees
START WITH employee_id = 101
CONNECT BY PRIOR employee_id = manager_id;Here, employee 101 is level 1. Its reports are level 2, and their reports are level 3. The column name distance_from_start makes the meaning clearer when the starting row is not the organizational root.
> callout note > LEVEL is calculated for the current traversal. It does not mean an employee's fixed organizational rank, job grade, or number of managers in every possible query.
Generate rows with `LEVEL`
LEVEL can also generate a sequence when used with DUAL. DUAL is Oracle's one-row system table.
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 5;This returns the numbers 1 through 5. The query starts with the one row in DUAL, then continues while the CONNECT BY condition allows another level.
Exercise
If an employee is selected by START WITH and has two direct reports, what LEVEL value does each direct report receive?
Show answer
Each direct report receives LEVEL 2. The selected starting employee is LEVEL 1.