Brightlane's QA system assigns consecutive numeric identifiers to each test run in a processing batch.
Write a query to return a sequence of integers from 1 through 5, with each value appearing as a separate row.
Output:
- Five rows, with one column,
test_id, containing the integers1through5in ascending order.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
GENERATE_SERIES(1, 5, 1) AS test_id The shape
generate_series(1, 5, 1) returns the numbers 1 through 5 as five separate rows, which is exactly the shape the QA system needs to assign one identifier per test run. The function expands into a set of rows in the FROM-like position of the query, so the result reads like a tiny one-column table.
Clause by clause
SELECT generate_series(1, 5, 1) AS test_idcalls the set-returning function with a start of1, an end of5, and a step of1. The function emits one row for each integer in the range. There is noFROMbecause the function is the source of rows; the values come from the function itself, not from a table.AS test_idlabels the output column so each row reads as a test identifier rather than the raw function name. Without the alias, PostgreSQL would name the columngenerate_series, which would be awkward to reference downstream.
Why this and not five SELECT statements stacked together
You could write five separate single-row queries and combine them, but that scales badly the moment the range changes. generate_series takes the range as arguments, so swapping 5 for 50 produces fifty rows with no extra typing. The function is also the canonical way to produce a sequence of values; readers of the query immediately recognise the shape.
You practiced generate_series(1, 5, 1) — the simplest set-returning function call; expands a range into one row per value.