Skip to content

RECURSIVE

WITH RECURSIVE numbers AS (
SELECT 1
UNION ALL
SELECT n+1 FROM numbers WHERE n < 10
)
SELECT * FROM numbers;
1
2
3
4
5
6
7
8
9
10

The WITH RECURSIVE clause creates a temporary named result set, numbers. The SELECT 1 initializes the first row of the recursive query. The UNION ALL SELECT n+1 FROM numbers WHERE n < 10 clause recursively generates the next row by adding 1 to the previous row value returned by the numbers result set. This continues until the value n reaches 10. The SELECT * FROM numbers statement then retrieves all of the values generated by the recursive query.