Sub topic 10
UNION versus UNION ALL inside recursion
Assume you know SELECT, joins, and common table expressions (CTEs). A recursive CTE has an anchor member that produces the first rows and a recursive member that produces the next rows. Between them, you choose UNION ALL or UNION.
The choice controls duplicate rows:
UNION ALLkeeps every row produced by every iteration.UNIONremoves duplicate rows from the recursive result.- A duplicate means that every selected column has the same value. SQL does not deduplicate by only one column unless you design the query to do so.
The usual choice: UNION ALL
UNION ALL is usually the best default for recursive queries. It preserves each path and avoids the work of comparing rows for duplicates.
WITH RECURSIVE numbers(i) AS (
SELECT 1
UNION ALL
SELECT i + 1
FROM numbers
WHERE i < 5
)
SELECT i
FROM numbers;The anchor returns 1. Each recursive iteration adds one, so the result is 1 through 5.
UNION ALL is important when repeated rows have meaning. For example, two different routes through a graph may reach the same city. If you include the route or path in the result, those are different rows even when their destination is equal.
When UNION can stop repetition
UNION performs duplicate elimination. In many database systems, recursive evaluation adds only rows that have not already appeared in the CTE result. This can make a reachability query over a finite graph stop when a cycle leads back to an existing row.
WITH RECURSIVE edges(src, dst) AS (
VALUES
(1, 2),
(2, 3),
(3, 1),
(2, 4)
),
reach(node) AS (
SELECT 1
UNION
SELECT e.dst
FROM reach r
JOIN edges e ON e.src = r.node
)
SELECT node
FROM reach
ORDER BY node;The result is 1, 2, 3, and 4. When recursion follows 3 -> 1, node 1 is already present, so that row does not create another iteration.
UNION is safe here because the CTE contains only the node. If you add a changing column, such as depth, the rows may never be duplicates:
-- A cycle can keep producing different (node, depth) rows.
SELECT e.dst, r.depth + 1(1, 0) and (1, 3) are different rows. Therefore, UNION cannot detect the cycle in this design.
Compare the choices
| Choice | Keeps duplicates | Preserves separate paths | Helps deduplicate cycles |
|---|---|---|---|
| `UNION ALL` | Yes | Yes | No |
| `UNION` | No | No, when selected columns match | Sometimes |
Use an explicit path check when you need both path information and cycle protection. For example, in PostgreSQL, an array can record visited nodes:
WITH RECURSIVE reach(node, path) AS (
SELECT 1, ARRAY[1]
UNION ALL
SELECT e.dst, r.path || e.dst
FROM reach r
JOIN edges e ON e.src = r.node
WHERE e.dst <> ALL (r.path)
)
SELECT node, path
FROM reach;Here, UNION ALL preserves each permitted path, while the WHERE condition prevents revisiting a node already in that path.
Exercise
Why does UNION not reliably prevent cycles when the recursive CTE selects both node and depth?
Show answer
Each visit can have a different depth, so the complete rows are different. Use a depth limit or a path check to prevent revisiting nodes.