Sub topic 3
What PRIOR Refers To
Assume you know basic SELECT, WHERE, and ORDER BY, primary and foreign keys, self-joins, and Oracle's DUAL table. This chapter focuses on how PRIOR connects one row to another in an Oracle hierarchical query.
Parent and child rows
A hierarchical query follows relationships between rows. A common example is an employees table where manager_id stores the employee_id of the employee's manager.
| Column | Meaning |
|---|---|
| `employee_id` | Identifies the employee |
| `last_name` | Stores the employee's name |
| `manager_id` | Identifies that employee's manager |
The table refers to itself. An employee row is a child of its manager row. The manager row is the parent.
PRIOR is an Oracle operator that tells CONNECT BY to evaluate an expression using the row that has already been reached. In a top-down query, that reached row is the parent. The other side of the condition is evaluated using a candidate child row.
SELECT employee_id, last_name, manager_id, LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;Read the CONNECT BY condition as:
> The parent's employee_id must equal the child's manager_id.
The query starts with employees whose manager_id is NULL. For each such root row, Oracle finds employees whose manager_id matches the root's employee_id. It then repeats this process for every child.
Which side does PRIOR apply to?
PRIOR is a unary operator. It applies to the expression immediately after it. In this condition, it applies to employee_id:
CONNECT BY PRIOR employee_id = manager_idThe two sides have different roles:
| Expression | Value comes from |
|---|---|
| `PRIOR employee_id` | The row already reached |
| `manager_id` | The candidate row being found |
The column names do not have to be different. The important part is where PRIOR appears.
A table alias can make the roles easier to see:
SELECT employee_id, last_name, manager_id, LEVEL
FROM employees e
START WITH e.manager_id IS NULL
CONNECT BY PRIOR e.employee_id = e.manager_id;The alias does not change the meaning. PRIOR e.employee_id still means the employee_id from the previously reached row.
Reversing the direction
Moving PRIOR changes the traversal direction:
SELECT employee_id, last_name, LEVEL
FROM employees
START WITH employee_id = 107
CONNECT BY employee_id = PRIOR manager_id;Here, Oracle starts at employee 107. The condition means:
> Find a row whose employee_id equals the current row's manager_id.
This walks from an employee up to that employee's manager, then to the manager's manager.
A practical reading rule
When checking a CONNECT BY condition, identify these parts:
- Which expression has
PRIOR? - Which row supplies that expression?
- Which column in the candidate row must match it?
- Does the match move from parent to child or from child to parent?
Exercise
In CONNECT BY PRIOR employee_id = manager_id, which row supplies employee_id, and what direction does the query normally traverse?
Show answer
The previously reached row supplies employee_id. The candidate row supplies manager_id, so the query normally traverses from a manager down to that manager's employees.