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 whose signup_date falls in the month;
  • churned_subscribers: subscribers whose cancel_date falls 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).

ColumnTypeDescription
subscriber_id (key)INTEGERUnique id
planTEXT'monthly' or 'quarterly'
signup_dateTEXTFirst day of the subscription (YYYY-MM-DD)
cancel_dateTEXTDay the subscription ended; the subscriber is no longer active from this day on. NULL while still subscribed

Example

box_subscribers

subscriber_idplansignup_datecancel_date
1monthly2025-11-20NULL
2monthly2026-01-052026-03-12
3quarterly2026-01-312026-01-31
4monthly2026-02-14NULL
5quarterly2025-12-012026-02-28
6monthly2026-04-022026-06-30
7monthly2026-07-01NULL

Expected output

report_monthactive_at_month_endnew_subscriberschurned_subscribers
2026-01321
2026-02311
2026-03201
2026-04310
2026-05300
2026-06201

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 report needs a row for every month even when the table is empty, so generate the six months yourself with a recursive CTE.
For each month compute its last day: date(month_start, '+1 month', '-1 day').
Active at month end: signed up on or before that day AND (cancel_date IS NULL OR cancel_date > that day). The IS NULL part is easy to forget.
New and churned are simple range checks on signup_date and cancel_date.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url