Sub topic 6
Sorting Within Branches with `ORDER SIBLINGS BY`
This chapter assumes that you know basic SELECT, WHERE, and ORDER BY, and that employees has a self-referencing relationship: manager_id stores the employee_id of an employee's manager. The previous chapter's hierarchical query returns rows in tree order. This chapter shows how to control the order of employees who share the same manager without breaking that tree structure.
Why ordinary `ORDER BY` is not enough
A hierarchical query has two kinds of order:
- Hierarchy order determines where a child appears relative to its parent.
- Sibling order determines how children of the same parent are arranged.
For example, if two employees report to the same manager, they are siblings in the hierarchy. Sorting all result rows with an ordinary ORDER BY last_name can place a child far away from its parent. ORDER SIBLINGS BY sorts rows only among siblings and preserves the parent-child structure.
SELECT employee_id,
last_name,
manager_id,
LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;Oracle first finds the tree, starting with employees whose manager_id is NULL. It then sorts each manager's direct reports by last_name. The descendants of each report remain directly below that report.
Combine sibling sorting with indentation
LEVEL contains the depth of the current row. You can use it with LPAD to make the hierarchy easier to read. LPAD adds characters to the left side of a string.
SELECT LPAD(' ', 2 * (LEVEL - 1)) || last_name AS org_chart,
LEVEL
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;The root has LEVEL 1, so it receives no indentation. A direct report has LEVEL 2 and receives two spaces. A grandchild has LEVEL 3 and receives four spaces.
You can sort siblings using multiple expressions, just as with an ordinary ORDER BY:
SELECT LPAD(' ', 2 * (LEVEL - 1)) || last_name AS org_chart,
salary
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY salary DESC, last_name ASC;This puts higher-paid employees first within each branch. Employees with the same salary are then sorted by last name.
| Clause | Effect |
|---|---|
| `ORDER SIBLINGS BY last_name` | Alphabetizes direct reports under each manager |
| `ORDER SIBLINGS BY salary DESC` | Places higher-paid siblings first |
| `ORDER SIBLINGS BY salary DESC, last_name` | Uses last name to break salary ties |
Common errors
Do not replace ORDER SIBLINGS BY with an ordinary ORDER BY when the tree structure matters. An ordinary ORDER BY orders the complete result set and can separate a manager from the manager's descendants.
Also, do not expect sibling ordering to sort every row globally. It is applied independently at each parent. A last name that appears first under one manager does not force that employee's entire branch to appear before every other branch.
Exercise
Which clause sorts employees alphabetically under each manager while preserving the hierarchy?
Show answer
ORDER SIBLINGS BY last_name.