A recursive query needs both an initial row and a terminating condition. Walk through the boundary to see whether zero is included.
Worked example
WITH RECURSIVE t(n) AS (VALUES(5) UNION ALL SELECT n-1 FROM t WHERE n>0) SELECT n FROM t;Result
[
{
"n": 5
},
{
"n": 4
},
{
"n": 3
},
{
"n": 2
},
{
"n": 1
},
{
"n": 0
}
]Implementation notes
Without a termination condition, recursive expansion can consume far more work than the result suggests. Bound a production traversal independently of sample data.
Try it yourself
Start at zero and at one and compare the final rows.
Open the SQL example →Editorial date December 3, 2025 · Published December 6, 2026