Skip to content

Recursive join

Repeatedly join relational query results to a relation to derive further rows from seeds, with duplicate and stopping rules determining whether a completed result is reached.

Core Idea

A recursive join is a relational query pattern that starts from seed rows and repeatedly joins rows derived at one step to another relation to derive further rows. It can expand an assembly into subparts or follow an employee reporting chain. A recursive calculation without such a join is not this narrower pattern. Whether the expression returns a completed result depends on the row shape, duplicate rule, data cycles, and stopping condition.[1][2]

How would you explain it like I'm…

The Helper-of-Helpers Finder

Imagine you want to find everyone who works under a boss — their helpers, their helpers' helpers, and so on. You start with the boss, look up who they're in charge of, then look up who those people are in charge of, again and again. A recursive join is a way for a computer database to do that step-by-step looking up.

Chain-Following Database Search

Databases store information in tables, and a 'join' connects rows from tables that match, like matching each worker to their boss. A recursive join starts with some starting rows, then repeatedly joins the rows it just found to a table to find more rows — like finding a boss's team, then each team member's team, and so on. It's used for things like following who-reports-to-whom chains or listing all the parts inside a machine. If the data loops back on itself and there's no stopping rule, the query could keep going forever.

Iterative Relational Joins

A recursive join is a database query pattern that starts with seed rows and then repeatedly joins the latest rows to a table to find more. It's used to follow chains, like an employee reporting hierarchy, or to break a product into its parts and subparts. What makes it a recursive join is that each repeated step includes a join with a table; a recursive query that just counts upward isn't one. How duplicates are handled matters: in PostgreSQL, UNION removes duplicate rows but UNION ALL keeps them. If rows carry extra info like depth or path, the same item can appear many times. With cycles in the data, an unguarded query can run forever, and a depth limit stops it but only answers a limited question.

 

A recursive join is a relational query pattern in which seed rows initialize a result and a recursive term repeatedly joins the working rows from the prior iteration to another relation to derive further rows, as in traversing a reporting hierarchy, exploding a bill of materials, or other multi-step relational derivations. Its defining feature is not mere self-reference but that the iterated step contains a join between recursive working rows and a relation; a WITH RECURSIVE arithmetic counter is recursive yet involves no such join. Results depend on row content and duplicate semantics: in PostgreSQL, UNION discards duplicate complete rows, while UNION ALL retains them. When rows carry depth, path, or quantity, repeated visits to the same underlying item can produce distinct rows, defeating duplicate elimination. On cyclic data an unguarded query can produce rows indefinitely, so writing a recursive join does not by itself yield a completed transitive closure. A depth cap terminates execution but answers only a depth-bounded question.

Scope of Application

PostgreSQL documents a product-parts query that joins previously included parts to a parts relation and multiplies quantities. Microsoft documents an employee-hierarchy query that joins prior DirectReports rows to an employee table. The two differ in data and output, but both have anchor rows, recursive working rows, a joined relation, and derived results. Their syntax and implementation details belong to the respective database systems.[1][2]

Clarity

Ask separately whether a query contains a recursive join and whether it finishes with the intended answer. UNION removes duplicate complete rows in PostgreSQL, whereas UNION ALL keeps them. If output includes changing depth or path values, even UNION may fail to suppress cyclic revisits. A depth limit can stop a query without computing full reachability.[1]

Manages Complexity

Unroll the operation as a small repeated state transition: establish anchors, join current working rows to the relation, project new rows, apply duplicate rules, and check for an empty next step or another declared stop. This reveals whether quantities or paths are meaningful and where a cycle could cause an unbounded result. There is no universal rule that every recursive join deduplicates or reaches a fixed point.[1][2]

Abstract Reasoning

To assess a query, identify its anchor and recursive term, then trace one row through the join predicate. If a relation contains a cycle, ask whether the projected whole row repeats or changes. If it changes, a visited-key, path, depth guard, or explicit cycle rule may be needed for a completed answer. Check whether the requested result is full closure, bounded traversal, or path/quantity enumeration before choosing the stopping and duplicate semantics.[1]

Knowledge Transfer

The same role map transfers between parts expansion and employee hierarchy inside relational-query work. Graph traversal outside a relational system may be analogous but is not literally a recursive join without a relation operand and join operation. The typed parent Relation is an internal constituent of every such join; the full database-specific operation is not itself a new Prime.[1][2]

Example

PostgreSQL's included_parts query anchors parts directly included in our_product. Its recursive term joins prior included_parts rows to parts where a prior subpart is the parent part, multiplies quantities, and finally sums totals by subpart. The anchors start the traversal, the working rows carry prior results, parts supplies the joined relation, and quantity is part of the derived row. Because this example uses UNION ALL and quantity-bearing rows, it should not be read as an automatic unique-node closure or a universal termination rule.[1]

Relationships to Other Abstractions

Local relationship map for Recursive joinParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Recursive joinDOMAINPrime abstraction: Relation — includesRelationPRIME

Current abstraction Recursive join Domain-specific

Parents (1) — outgoing typed relations

  • Recursive join includes Relation Prime

    A recursive join contains a relation as the operand repeatedly matched against recursively derived rows.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Recursive join sits in a sparse region of the domain-specific corpus (97th percentile for distinctiveness): few abstractions share its structure, so a faithful description tends to retrieve it precisely.

Family — Unclustered & Miscellaneous (3155 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

Not to Be Confused With

An ordinary join runs once rather than feeding derived rows into another join. A general recursive CTE may compute arithmetic without a relational join. Transitive closure is a possible completed reachability result, not the identity or guaranteed outcome of every recursive-join expression. Query rewriting transforms an expression into an equivalent form; recursive join evaluates a repeated relational derivation. The approved strict DAG edge is composition/part_of to Relation, with the relation inside the query; live Recursion, Iteration, and Fixed Point are not asserted as all-instance parents because unsafe cyclic queries may fail their stopping or convergence conditions.[1][2]

References

[1] PostgreSQL Global Development Group. PostgreSQL 18 Documentation, 7.8 WITH Queries (Common Table Expressions). §§7.8.2 and 7.8.2.2. Anchor/recursive-term evaluation; UNION versus UNION ALL; product-parts example; cycle-detection example. registry ↩a ↩b ↩c ↩d ↩e ↩f ↩g ↩h

[2] Microsoft. Recursive queries using common table expressions (Transact-SQL). Last updated 2026-09-21. Structure, Pseudocode and Semantics, and employee-hierarchy example with code walkthrough. registry ↩a ↩b ↩c ↩d ↩e