Example 1
Generate the integers 1 through 5
In the recursive CTE, the anchor SELECT 1 produces the first row. The recursive branch adds 1 to the previous value and the WHERE n < 5 condition stops the recursion once the sequence reaches 5. PostgreSQL's generate_series is a single function call that handles start, stop, and an optional step argument — for example generate_series(0, 10, 2) produces even numbers from 0 to 10.
PostgreSQL uses the built-in `generate_series(1, 5)` function. MySQL, MariaDB, and SQLite use `WITH RECURSIVE`. SQL Server also uses a recursive CTE but omits the `RECURSIVE` keyword. All produce the same five-row sequence.