N034-H1 Tier 3 · Intermediate · hard ecommerce · Brightlane

Return the date exactly `1` calendar month after `'2024-01-31'`

Part of Date Arithmetic and Intervals in SQL

The problem

Brightlane's subscription platform schedules monthly renewals. A subscriber's most recent renewal was on '2024-01-31', and the team needs the date of the next renewal one calendar month later.

Write a query to return the date exactly 1 calendar month after '2024-01-31'.

Output:

  • A single row with one column, next_renewal, containing the renewal date. 2024 is a leap year, so the result is the last valid day of February in that year.
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
  '2024-01-31'::date + INTERVAL '1 month' AS next_renewal

The shape

Adding INTERVAL '1 month' to January 31, 2024 returns February 29, 2024. PostgreSQL preserves the day-of-month when the target month has it, and clamps to the last valid day when it does not. February has at most 29 days in 2024, so the result clamps to the 29th.

Clause by clause

  • SELECT '2024-01-31'::date + INTERVAL '1 month' AS next_renewal builds the renewal date. The ::date cast types the literal as a calendar date. Adding a calendar-month INTERVAL advances the month component by one. PostgreSQL then resolves the day-of-month: January's 31 maps to February's last valid day, which is the 29th in this leap year. The alias AS next_renewal names the output for the subscription scheduler.

Why the result clamps and does not error

PostgreSQL treats month arithmetic as best-effort, not exact. There is no February 31, so an exact match is impossible. The rule the database applies is to round down to the last day that exists in the target month. No error fires, no NULL is returned, and the query produces a usable date. The clamping behavior is deterministic and documented, but it is silent.

The trap

A monthly renewal schedule built by repeatedly adding INTERVAL '1 month' to the original sign-up date can drift in ways that surprise the business. A subscriber who signed up on January 31 renews on February 29 in 2024, then on March 29 in the next step rather than returning to the 31st, because each step adds one month to whatever the previous result was, not to the original anchor. For consistent month-end renewals, anchor each step at the original date or use a date-truncation construction instead of chaining month additions.

You practiced date + INTERVAL '1 month' against a month-end starting date — when the target month has no matching day-of-month, PostgreSQL clamps the result to the last valid day of that month.

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.