Brightlane's subscription billing system computes the start of the previous billing period by subtracting one calendar month from the current period's start date.
Write a query to return the date produced by subtracting 1 month from '2024-06-15'.
Output:
- A single row with one column,
prev_period_start, containing the resulting date.
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
'2024-06-15'::date - INTERVAL '1 month' AS prev_period_start The shape
Subtracting INTERVAL '1 month' from a date walks the calendar back one month and returns the resulting date. Because June 15 has a matching day in May, the result lands cleanly on May 15, 2024.
Clause by clause
SELECT '2024-06-15'::date - INTERVAL '1 month' AS prev_period_startbuilds the previous billing period's start date. The::datecast types the literal as a calendar date, the-operator subtracts the duration, andINTERVAL '1 month'names the unit as a calendar month rather than a fixed day count. The aliasAS prev_period_startlabels the output column for the billing system to consume.
Why INTERVAL '1 month' and not INTERVAL '30 days'
The two intervals are different values. '1 month' is a calendar-month duration: PostgreSQL preserves the day-of-month and changes only the month component. '30 days' is a fixed-day duration: it advances the date by exactly thirty days, regardless of how many days the target month happens to have. Subtracting one month from June 15 returns May 15. Subtracting thirty days from June 15 returns May 16. For billing periods that align to calendar months, the calendar-month spelling is the correct one.
You practiced date - INTERVAL '1 month' — subtract a calendar-month duration; PostgreSQL preserves the day-of-month when the target month has it.