sqlcmd.net validated sql reference
advanced composition MySQL MariaDB SQL Server PostgreSQL SQLite

Generate a Sequence of Numbers

Produce a consecutive sequence of integers without a base table — using `generate_series` in PostgreSQL or a recursive CTE in all other engines.

Docker-validated Not currently validation-green

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.

Where this command helps.

  • generating a date range by adding a number sequence to a start date
  • producing test rows without needing a real table

What the command is doing.

PostgreSQL provides a built-in generate_series(start, stop) set-returning function that produces one row per integer in the range. Every other major engine achieves the same result with a recursive CTE: an anchor selects the first number, and the recursive branch increments it by one until the upper bound is reached. Number sequences are useful for generating test data, filling gaps in time series, or driving a cross-join to produce combinations without any physical table.