N051-H1 Tier 4 · Advanced · hard ecommerce · Brightlane

Return the total number of rows produced when `generate_series` is called with a start of `'2024-03-01'`, an end of `'2024-02-01'`, and a step of `1` day

Part of generate_series() for Sequences and Date Spines in SQL

The problem

Brightlane's billing system generates day sequences for contract periods. A developer is testing what generate_series produces when the end date precedes the start date — a condition that can occur if a contract record has its dates inverted.

Write a query to return the total number of rows produced when generate_series is called with a start of '2024-03-01', an end of '2024-02-01', and a step of 1 day.

Assumptions:

  • For these inputs, generate_series produces an empty result with no error raised.
  • Wrapping the empty result in COUNT(*) yields a single row with the count 0.

Output:

  • A single row with one column, row_count, containing the integer 0.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  COUNT(*) AS row_count
FROM
  (
    SELECT
      GENERATE_SERIES('2024-03-01'::date, '2024-02-01'::date, INTERVAL '1 day')::date AS DAY
  ) gs

The shape

generate_series walks forward from the start by the step until the value exceeds the end. When the end already precedes the start and the step is positive, the very first comparison fails and the function emits no rows. Wrapping that empty set in a derived table and counting it returns a single row with row_count = 0.

Clause by clause

  • The inner SELECT generate_series('2024-03-01'::date, '2024-02-01'::date, interval '1 day')::date AS day calls the function with a start that is one month past the end. The first candidate value is '2024-03-01', which already exceeds the end of '2024-02-01', so the function returns immediately with zero rows. No error is raised; the result is simply an empty set.
  • Wrapping that empty inner query in ( ... ) gs turns the zero-row result into a derived table the outer query can read from.
  • SELECT COUNT(*) AS row_count FROM (...) gs aggregates over the empty derived table. COUNT(*) over zero rows returns 0, and aggregates always produce exactly one row even when the input is empty, so the outer query returns a single row with the count 0.

Why this and not relying on generate_series to error or produce a backwards range

generate_series does not error on an inverted range with a positive step. It also does not silently flip the bounds and walk backward; that would require a negative step. The function takes the comparison literally: start, step forward, stop when the next candidate would exceed the end. With start > end and a positive step, "the next candidate would exceed the end" is true at iteration zero, so the loop never enters its body. A negative step like interval '-1 day' is what walks the range in reverse.

The trap

The behaviour is silent. There is no warning that the inputs are inverted, and a downstream query that reports "zero rows in range" looks identical whether the data legitimately had no activity or the date arguments were swapped by mistake. On any pipeline where the start and end come from another query or a parameter, validate that start <= end before calling generate_series, or check that the row count matches the expected length of the window. A zero-row spine produces a zero-row report, which is rarely what the analyst meant.

You practiced verifying generate_series boundary behavior — when the end precedes the start with a positive step, the function returns zero rows rather than failing or producing a degenerate range.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.