Plant Box Monthly Churn
A houseplant subscription box needs a monthly subscriber report for January to June 2026.
For each of those six months return:
active_at_month_end: subscribers active on the month's last day, meaning they signed up on or before that day and either never cancelled or cancelled after that day;new_subscribers: subscribers whosesignup_datefalls in the month;churned_subscribers: subscribers whosecancel_datefalls in the month.
A subscriber who joins and cancels in the same month counts as both new and churned. Columns: report_month (text 'YYYY-MM'), active_at_month_end, new_subscribers, churned_subscribers. Always return exactly six rows ordered by report_month, using 0 for months with no activity.
Tables
box_subscribers — One row per subscriber (a subscriber subscribes at most once).
| Column | Type | Description |
|---|---|---|
subscriber_id (key) | INTEGER | Unique id |
plan | TEXT | 'monthly' or 'quarterly' |
signup_date | TEXT | First day of the subscription (YYYY-MM-DD) |
cancel_date | TEXT | Day the subscription ended; the subscriber is no longer active from this day on. NULL while still subscribed |
Example
box_subscribers
| subscriber_id | plan | signup_date | cancel_date |
|---|---|---|---|
| 1 | monthly | 2025-11-20 | NULL |
| 2 | monthly | 2026-01-05 | 2026-03-12 |
| 3 | quarterly | 2026-01-31 | 2026-01-31 |
| 4 | monthly | 2026-02-14 | NULL |
| 5 | quarterly | 2025-12-01 | 2026-02-28 |
| 6 | monthly | 2026-04-02 | 2026-06-30 |
| 7 | monthly | 2026-07-01 | NULL |
Expected output
| report_month | active_at_month_end | new_subscribers | churned_subscribers |
|---|---|---|---|
| 2026-01 | 3 | 2 | 1 |
| 2026-02 | 3 | 1 | 1 |
| 2026-03 | 2 | 0 | 1 |
| 2026-04 | 3 | 1 | 0 |
| 2026-05 | 3 | 0 | 0 |
| 2026-06 | 2 | 0 | 1 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Product Analytics · problem 68 of 70 · SQLite
The idea
Two ideas carry this report: a calendar spine and clear interval rules.
Spine. Months with no activity still need a row, and the data cannot supply months it does not contain. A recursive CTE generates 2026-01-01 to 2026-06-01, and date(month_start, '+1 month', '-1 day') gives each month's last day. ISO dates compare correctly as text, so BETWEEN month_start AND month_end is a clean in-month test.
Rules. A subscription covers the half-open interval [signup_date, cancel_date): the cancel day itself is not active. So a subscriber is active at month end when signup_date <= month_end and the cancel date is missing or later than month_end. Write the IS NULL branch explicitly: a comparison with NULL is never true.
The solution uses one correlated COUNT(*) per metric, which returns 0 rather than NULL when nothing matches. The alternative LEFT JOINs subscribers onto the months and counts with COUNT(CASE ...). The left join keeps every month, and COUNT of an all-NULL group is 0.
Solution
WITH RECURSIVE months(month_start) AS (
SELECT '2026-01-01'
UNION ALL
SELECT date(month_start, '+1 month')
FROM months
WHERE month_start < '2026-06-01'
),
bounds AS (
SELECT month_start,
date(month_start, '+1 month', '-1 day') AS month_end
FROM months
)
SELECT
strftime('%Y-%m', b.month_start) AS report_month,
(SELECT COUNT(*) FROM box_subscribers s
WHERE s.signup_date <= b.month_end
AND (s.cancel_date IS NULL OR s.cancel_date > b.month_end)) AS active_at_month_end,
(SELECT COUNT(*) FROM box_subscribers s
WHERE s.signup_date BETWEEN b.month_start AND b.month_end) AS new_subscribers,
(SELECT COUNT(*) FROM box_subscribers s
WHERE s.cancel_date BETWEEN b.month_start AND b.month_end) AS churned_subscribers
FROM bounds AS b
ORDER BY report_month;
Another way
WITH RECURSIVE months(n) AS (
SELECT 1 UNION ALL SELECT n + 1 FROM months WHERE n < 6
),
bounds AS (
SELECT printf('2026-%02d', n) AS report_month,
date(printf('2026-%02d-01', n), '+1 month', '-1 day') AS month_end
FROM months
)
SELECT
b.report_month,
COUNT(CASE WHEN s.signup_date <= b.month_end
AND COALESCE(s.cancel_date, '9999-12-31') > b.month_end THEN 1 END) AS active_at_month_end,
COUNT(CASE WHEN substr(s.signup_date, 1, 7) = b.report_month THEN 1 END) AS new_subscribers,
COUNT(CASE WHEN substr(s.cancel_date, 1, 7) = b.report_month THEN 1 END) AS churned_subscribers
FROM bounds AS b
LEFT JOIN box_subscribers AS s
ON s.signup_date <= b.month_end
GROUP BY b.report_month
ORDER BY b.report_month;
Why common answers fail
Treats a subscriber who cancels on the last day of the month as still active at month end (>= instead of >).
WITH RECURSIVE months(month_start) AS (
SELECT '2026-01-01' UNION ALL SELECT date(month_start, '+1 month') FROM months WHERE month_start < '2026-06-01'
), bounds AS (SELECT month_start, date(month_start, '+1 month', '-1 day') AS month_end FROM months)
SELECT strftime('%Y-%m', b.month_start) AS report_month,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date <= b.month_end AND (s.cancel_date IS NULL OR s.cancel_date >= b.month_end)) AS active_at_month_end,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date BETWEEN b.month_start AND b.month_end) AS new_subscribers,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.cancel_date BETWEEN b.month_start AND b.month_end) AS churned_subscribers
FROM bounds b ORDER BY report_month;
cancel_date > month_end is NULL for subscribers who never cancelled, so the most loyal subscribers are dropped from the active count.
WITH RECURSIVE months(month_start) AS (
SELECT '2026-01-01' UNION ALL SELECT date(month_start, '+1 month') FROM months WHERE month_start < '2026-06-01'
), bounds AS (SELECT month_start, date(month_start, '+1 month', '-1 day') AS month_end FROM months)
SELECT strftime('%Y-%m', b.month_start) AS report_month,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date <= b.month_end AND s.cancel_date > b.month_end) AS active_at_month_end,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date BETWEEN b.month_start AND b.month_end) AS new_subscribers,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.cancel_date BETWEEN b.month_start AND b.month_end) AS churned_subscribers
FROM bounds b ORDER BY report_month;
Builds the months from the signup data, so months without a new signup disappear instead of showing zeros.
SELECT substr(signup_date, 1, 7) AS report_month,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date <= date(substr(o.signup_date, 1, 7) || '-01', '+1 month', '-1 day')
AND (s.cancel_date IS NULL OR s.cancel_date > date(substr(o.signup_date, 1, 7) || '-01', '+1 month', '-1 day'))) AS active_at_month_end,
COUNT(*) AS new_subscribers,
(SELECT COUNT(*) FROM box_subscribers s WHERE substr(s.cancel_date, 1, 7) = substr(o.signup_date, 1, 7)) AS churned_subscribers
FROM box_subscribers o
WHERE signup_date BETWEEN '2026-01-01' AND '2026-06-30'
GROUP BY substr(signup_date, 1, 7)
ORDER BY report_month;
Only counts subscribers who signed up during the month as active, forgetting everyone who joined earlier and is still subscribed.
WITH RECURSIVE months(month_start) AS (
SELECT '2026-01-01' UNION ALL SELECT date(month_start, '+1 month') FROM months WHERE month_start < '2026-06-01'
), bounds AS (SELECT month_start, date(month_start, '+1 month', '-1 day') AS month_end FROM months)
SELECT strftime('%Y-%m', b.month_start) AS report_month,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date BETWEEN b.month_start AND b.month_end AND (s.cancel_date IS NULL OR s.cancel_date > b.month_end)) AS active_at_month_end,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.signup_date BETWEEN b.month_start AND b.month_end) AS new_subscribers,
(SELECT COUNT(*) FROM box_subscribers s WHERE s.cancel_date BETWEEN b.month_start AND b.month_end) AS churned_subscribers
FROM bounds b ORDER BY report_month;
What the interviewer is checking
Subscription metrics test date spines, interval logic and NULL handling. The interviewer wants precise definitions, especially for boundary days and open-ended subscriptions.
Mistakes to avoid
- Deriving the month list from the data, so quiet months vanish.
- Forgetting cancel_date IS NULL, which drops active subscribers.
- Off-by-one on the cancel day (>= vs >).
- Counting only this month's signups as active.
Follow-ups to expect
- Add a churn rate: churned / active at the start of the month (use the previous month's active_at_month_end via LAG).
- Make the window dynamic: from the first signup month to the current month (seed the recursion with MIN(signup_date)).
- Break the report down by plan (cross join months with plans, add plan to each filter).
In real work
Subscription businesses report active, gross adds and churn monthly; the same spine-plus-interval pattern powers MRR and headcount reports.