Skip to content

Hierarchical queries (CONNECT BY)

View as Markdown

A hierarchical query returns the rows of a table that stores a tree, such as categories and subcategories, employees and managers, or parts and assemblies, in tree order. Each row points to its parent through a column; the START WITH clause chooses the root rows and the CONNECT BY clause says how a child row relates to its parent.

CoraDB also offers two other ways to walk connected data: recursive CTEs (standard SQL) and, for data stored as vertices and edges, MATCH path patterns.

SELECT column_list
FROM table_joins | tables
[WHERE join_conditions and/or filtering_conditions]
[START WITH condition] CONNECT BY [NOCYCLE] condition
[ORDER SIBLINGS BY col [ASC | DESC], ... | ORDER BY ...]

The two clauses can also be written in the order CONNECT BY ... START WITH ....

  • START WITH condition: the rows that satisfy the condition are the roots of the hierarchy. If you omit START WITH, or no row satisfies it, every row is a root, and the subtree below each row is returned.
  • CONNECT BY condition: the parent-child relationship. It is tested for pairs of rows; inside it, PRIOR marks the expression that is taken from the parent row. CONNECT BY PRIOR id = parent_id means "a row is a child of the row whose id equals its parent_id".
  • NOCYCLE: continue when the data contains a loop instead of failing (see Cycles).
  • ORDER SIBLINGS BY: sort the children of each parent while keeping the tree order (see Ordering).

The e-commerce categories have no built-in hierarchy, so the examples on this page add one in a scratch table: category_tree stores, for each category id, the id of its parent category. Joined with category, it gives three small trees: Men (with Tops and Bottoms), Gear and Sale.

CREATE TABLE category_tree (category_id INT PRIMARY KEY, parent_id INT);
INSERT INTO category_tree VALUES
(9, NULL), (10, 9), (11, 10), (14, 10), (23, 10), (27, 10),
(15, 9), (16, 15), (21, 15),
(28, NULL), (31, 28), (29, 28), (30, 28),
(32, NULL), (26, 32), (22, 32);

Start from the top-level categories and follow the parent-child links. The LEVEL pseudocolumn gives the depth of each row:

SELECT t.category_id, t.parent_id, c.name, LEVEL
FROM category_tree t JOIN category c ON c.id = t.category_id
START WITH t.parent_id IS NULL
CONNECT BY PRIOR t.category_id = t.parent_id
ORDER SIBLINGS BY c.name;
category_id parent_id name level
28 NULL Gear 1
31 28 Bags 2
29 28 Fitness Equipment 2
30 28 Watches 2
9 NULL Men 1
15 9 Bottoms 2
16 15 Pants 3
21 15 Shorts 3
10 9 Tops 2
11 10 Hoodies & Sweatshirts 3
14 10 Jackets 3
27 10 Tanks 3
23 10 Tees 3
32 NULL Sale 1
26 32 Men Sale 2
22 32 Women Sale 2

Without START WITH, every row becomes a root and the query returns each row once for every root above it: 35 rows instead of 16. Tees, for example, appears three times: as a root, under Tops and under Men.

When the FROM clause joins tables, as above, CoraDB evaluates the join first, then builds the hierarchy with START WITH and CONNECT BY, and applies the other WHERE conditions last, to the rows of the finished tree. A row removed by WHERE therefore does not remove its children:

SELECT c.name, LEVEL
FROM category_tree t JOIN category c ON c.id = t.category_id
WHERE c.name <> 'Tops'
START WITH t.category_id = 9
CONNECT BY PRIOR t.category_id = t.parent_id
ORDER SIBLINGS BY c.name;
name level
Men 1
Bottoms 2
Pants 3
Shorts 3
Hoodies & Sweatshirts 3
Jackets 3
Tanks 3
Tees 3

Tops is gone, but its four children are still there at level 3. To leave out a whole subtree, put the condition in CONNECT BY instead. Write join conditions in the ON clause of the join rather than in WHERE, so that they cannot be mistaken for filters.

A plain ORDER BY sorts the result as a flat list and loses the tree order. ORDER SIBLINGS BY keeps every child below its parent and sorts only the children of the same parent, as in the examples above, where the siblings are listed by name.

ORDER SIBLINGS BY col_1 [ASC | DESC] [, col_2 [ASC | DESC] ...]

LEVEL is the depth of the row in the tree: 1 for a root, 2 for its children, and so on. It can be used in the select list, WHERE, ORDER BY, GROUP BY ... HAVING and in aggregate functions. List only the second level:

SELECT c.name, LEVEL
FROM category_tree t JOIN category c ON c.id = t.category_id
WHERE LEVEL = 2
START WITH t.parent_id IS NULL
CONNECT BY PRIOR t.category_id = t.parent_id
ORDER BY c.name;
name level
Bags 2
Bottoms 2
Fitness Equipment 2
Men Sale 2
Tops 2
Watches 2
Women Sale 2

LEVEL in the CONNECT BY condition is a quick way to generate a number of rows from the one-row system table db_root:

SELECT LEVEL FROM db_root CONNECT BY LEVEL <= 5;
level
1
2
3
4
5

The same trick produces a series of dates, for example the first week of December 2025:

SELECT TO_CHAR(DATE'2025-12-01' + LEVEL - 1, 'YYYY-MM-DD') AS order_day
FROM db_root
CONNECT BY LEVEL <= 7;
order_day
2025-12-01
2025-12-02
2025-12-03
2025-12-04
2025-12-05
2025-12-06
2025-12-07

CONNECT_BY_ISLEAF is 1 when the row has no children in the result, and 0 otherwise. It appears in the example in Operators and functions below.

CONNECT_BY_ISCYCLE is 1 when the row has a child that is also one of its ancestors, that is, where a loop was cut, and 0 otherwise. It is available only with NOCYCLE; see Cycles.

  • PRIOR expr: the value of expr in the parent row, NULL for a root. Besides CONNECT BY, it can be used in the select list, WHERE and ORDER BY.
  • CONNECT_BY_ROOT expr: the value of expr in the root row of the current tree. It can be used in the select list, WHERE and ORDER BY.
  • SYS_CONNECT_BY_PATH(column, separator): the path from the root to the current row, made of the column's values, each preceded by the separator. Both arguments must be character strings.

The query below shows all of them for the Men tree:

SELECT c.name,
CONNECT_BY_ISLEAF AS is_leaf,
CONNECT_BY_ROOT c.name AS root,
PRIOR c.name AS parent,
SYS_CONNECT_BY_PATH(c.name, '/') AS path
FROM category_tree t JOIN category c ON c.id = t.category_id
START WITH t.category_id = 9
CONNECT BY PRIOR t.category_id = t.parent_id
ORDER SIBLINGS BY c.name;
name is_leaf root parent path
Men 0 Men NULL /Men
Bottoms 0 Men Men /Men/Bottoms
Pants 1 Men Bottoms /Men/Bottoms/Pants
Shorts 1 Men Bottoms /Men/Bottoms/Shorts
Tops 0 Men Men /Men/Tops
Hoodies & Sweatshirts 1 Men Tops /Men/Tops/Hoodies & Sweatshirts
Jackets 1 Men Tops /Men/Tops/Jackets
Tanks 1 Men Tops /Men/Tops/Tanks
Tees 1 Men Tops /Men/Tops/Tees

If the data contains a loop, following the links would never end. Make Men a child of Tees, which is itself below Men:

UPDATE category_tree SET parent_id = 23 WHERE category_id = 9;

Without NOCYCLE, CoraDB detects the loop and cancels the query:

SELECT c.name
FROM category_tree t JOIN category c ON c.id = t.category_id
START WITH t.category_id = 9
CONNECT BY PRIOR t.category_id = t.parent_id;
ERROR: CONNECT BY loop in user data.

With NOCYCLE, CoraDB stops following a branch when it would come back to a row that is already on the current path, and returns the rest. CONNECT_BY_ISCYCLE marks the row where the loop was cut:

SELECT c.name, LEVEL,
CONNECT_BY_ISCYCLE AS is_cycle,
SYS_CONNECT_BY_PATH(c.name, '/') AS path
FROM category_tree t JOIN category c ON c.id = t.category_id
START WITH t.category_id = 9
CONNECT BY NOCYCLE PRIOR t.category_id = t.parent_id
ORDER SIBLINGS BY c.name;
name level is_cycle path
Men 1 0 /Men
Bottoms 2 0 /Men/Bottoms
Pants 3 0 /Men/Bottoms/Pants
Shorts 3 0 /Men/Bottoms/Shorts
Tops 2 0 /Men/Tops
Hoodies & Sweatshirts 3 0 /Men/Tops/Hoodies & Sweatshirts
Jackets 3 0 /Men/Tops/Jackets
Tanks 3 0 /Men/Tops/Tanks
Tees 3 1 /Men/Tops/Tees

A CONNECT BY query does extra work for every row (cycle detection and the pseudocolumns). When it returns most of a table, a query that joins the table to itself a fixed number of times can be faster. When it returns a small subtree of a large table, START WITH ... CONNECT BY is usually faster, especially with an index on the parent column, because only the rows below the roots are visited.