Subscriptions over time
A subscription is a period, not a single moment. Q03 and Q04 count the periods active at a moment, Q06 measures the break between two periods, and Q07 reads the latest row of each customer. The traps sit at the boundaries: the end of a period, a refund, an empty day, two rows with the same time. The background is on Calendars and active periods, Top N per group and Shares and running totals, where LAG is explained.
Q03. Active paid subscriptions on a date
Section titled “Q03. Active paid subscriptions on a date”How many paid subscriptions were active on 2026-09-01 00:00 UTC, per product?
Output columns: product_id, active_subscriptionsOrder: ORDER BY active_subscriptions DESC, product_id
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
What does "active" mean for one transaction?
start_time <= moment < end_time (half-open interval).
Paid means?
Not a trial, not a test purchase, channel is not 'promo'.
What about refunds?
A refunded transaction stops being active at refunded_at. If refunded_at <= moment, it is not active.
Products with zero?
Leave them out.
About the data: transactions
- One row in transactions is one subscription period: a purchase, a renewal or a trial. Renewals of the same subscription are separate rows.
- A period is active at moment m if start_time <= m < end_time.
- A refund ends the period: from refunded_at on, it is not active.
- A paid row is not a trial, not a test purchase, and channel <> 'promo' (promo means free access).
- In the example, period t_000144 runs until October 13, 2025, but it was refunded on September 20 — from September 20 it is no longer active.
A few rows
SELECT transaction_id AS id, original_transaction_id AS chain, is_trial AS trial, is_trial_conversion AS conversion, renewal_number AS n, start_time, end_time, revenue_usd AS net, refunded_atFROM transactionsWHERE original_transaction_id IN ('t_000141', 't_003269')ORDER BY chain, start_time;| id | chain | trial | conversion | n | start_time | end_time | net | refunded_at |
|---|---|---|---|---|---|---|---|---|
| t_000141 | t_000141 | true | false | 1 | 2025-07-06 17:45:04 | 2025-07-13 17:45:04 | 0.00 | NULL |
| t_000142 | t_000141 | false | true | 2 | 2025-07-13 17:45:04 | 2025-08-13 17:45:04 | 4.99 | NULL |
| t_000143 | t_000141 | false | false | 3 | 2025-08-13 17:45:04 | 2025-09-13 17:45:04 | 4.99 | NULL |
| t_000144 | t_000141 | false | false | 4 | 2025-09-13 17:45:04 | 2025-10-13 17:45:04 | 0.00 | 2025-09-20 20:52:05 |
| t_003269 | t_003269 | true | false | 1 | 2026-02-24 11:20:41 | 2026-03-03 11:20:41 | 0.00 | NULL |
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
Write the condition for one row first: when is a period active at exactly 1 September 00:00? Then add what makes it paid and what a refund changes.
Say it: A normal index helps only half of this condition. A range column with a GiST index answers 'active at a moment' directly.
Reference solution
SELECT product_id, COUNT(*) AS active_subscriptionsFROM transactionsWHERE start_time <= TIMESTAMP '2026-09-01' AND end_time > TIMESTAMP '2026-09-01' AND NOT is_trial AND NOT is_test AND channel <> 'promo' AND (refunded_at IS NULL OR refunded_at > TIMESTAMP '2026-09-01')GROUP BY product_idORDER BY active_subscriptions DESC, product_id;Expected output
| product_id | active_subscriptions |
|---|---|
| prd_08 | 69 |
| prd_20 | 64 |
| prd_04 | 62 |
| prd_03 | 57 |
| prd_11 | 57 |
| prd_14 | 57 |
| prd_18 | 57 |
| prd_07 | 55 |
| prd_12 | 54 |
| prd_16 | 54 |
| prd_10 | 53 |
| prd_05 | 51 |
| prd_15 | 51 |
| prd_06 | 50 |
| prd_01 | 48 |
| prd_13 | 48 |
| prd_19 | 47 |
| prd_17 | 46 |
| prd_02 | 44 |
| prd_09 | 44 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. How many paying subscribers each product had on 1 September. This number is the base of MRR and churn. A period is active at moment m if m is inside [start_time, end_time).
Steps. One WHERE and one GROUP BY product_id:
- active:
start_time <= m AND end_time > m; - paid: not a trial, not a test purchase, not promo access;
- a refund before the moment cancels the period:
refunded_at IS NULL OR refunded_at > m.
Traps.
end_time >= mcounts a period that ended exactly at that midnight, andrefunded_at >= mkeeps one refunded exactly then. No practice row sits on this boundary. The hidden edge-case data that Check also uses has both cases, so either mistake fails there.- A plain
refunded_at IS NULLdrops periods refunded after 1 September. At that moment they were still active. Here it loses 4 subscriptions: 1064 instead of 1068.
Check. The first row is prd_08 with 69. The sum over all products is 1068, the same as the first day of Q04.
Follow-up. “And for every day of September?” That is Q04: a calendar and the same count for each day.
In production. start_time <= m AND end_time > m is two ranges. A B-tree on start_time cuts off the future periods, but every past period must still be read. For “active at a moment”, a range column tsrange(start_time, end_time) with a GiST index and the @> operator answers the question directly.
Q04. Daily active subscriptions — September 2026
Section titled “Q04. Daily active subscriptions — September 2026”For every day of September 2026, show how many paid subscriptions were active at 00:00 UTC of that day. Every day must appear, even with 0.
Output columns: day (DATE), active_subscriptionsOrder: ORDER BY day
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
Same definition of active and paid as in Q03?
Yes.
Days?
2026-09-01 … 2026-09-30, 30 rows. Hint: generate_series.
Why not GROUP BY start_time?
A subscription is active on many days, not only on the day it started.
Can I simply repeat the Q03 count for every day?
Yes, any correct way is fine.
About the data: transactions
- Active at moment m works as in q03: start_time <= m < end_time, and a refund ends the period.
- A paid row is not a trial, not a test purchase, and channel <> 'promo' (promo means free access).
- Every day, even a day with no data, comes from a calendar: generate_series(start, end, INTERVAL '1 day').
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
You already have the count for one moment from Q03. Generate the 30 days first, attach that count to each day, and think about a day with no rows at all.
Say it: In production I would precompute a daily snapshot instead of scanning all transactions for every day.
Reference solution
WITH days AS ( SELECT d AS day_start FROM generate_series(TIMESTAMP '2026-09-01', TIMESTAMP '2026-09-30', INTERVAL '1 day') AS g(d))SELECT CAST(days.day_start AS DATE) AS day, COUNT(t.transaction_id) AS active_subscriptionsFROM daysLEFT JOIN transactions t ON t.start_time <= days.day_start AND t.end_time > days.day_start AND NOT t.is_trial AND NOT t.is_test AND t.channel <> 'promo' AND (t.refunded_at IS NULL OR t.refunded_at > days.day_start)GROUP BY days.day_startORDER BY day;Expected output
| day | active_subscriptions |
|---|---|
| 2026-09-01 | 1068 |
| 2026-09-02 | 1063 |
| 2026-09-03 | 1063 |
| 2026-09-04 | 1064 |
| 2026-09-05 | 1066 |
| 2026-09-06 | 1066 |
| 2026-09-07 | 1068 |
| 2026-09-08 | 1068 |
| 2026-09-09 | 1067 |
| 2026-09-10 | 1071 |
| 2026-09-11 | 1072 |
| 2026-09-12 | 1076 |
| 2026-09-13 | 1079 |
| 2026-09-14 | 1079 |
| 2026-09-15 | 1075 |
| 2026-09-16 | 1074 |
| 2026-09-17 | 1068 |
| 2026-09-18 | 1063 |
| 2026-09-19 | 1064 |
| 2026-09-20 | 1067 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. The same metric as Q03, but for each day. This is the line on an “active subscriptions” chart.
Steps. A calendar of days from generate_series, and for each day the count from Q03. There are two ways:
- simpler: a subquery in
SELECT,(SELECT COUNT(*) FROM transactions t WHERE … d …), once per day; - classic, as in the reference: the calendar
LEFT JOIN transactionson the condition “active on this day”, thenCOUNT(t.transaction_id).
Traps.
- With
LEFT JOIN, every condition ontransactionsgoes intoON, not intoWHERE. Otherwise the empty days disappear. COUNT(t.transaction_id), notCOUNT(*): an empty day has one row with NULLs, andCOUNT(*)shows 1 for it.
Check. 30 rows. 1 September — 1068, 2 September — 1063, 30 September — 1044. Every practice day has more than 1000 active subscriptions, so neither trap changes the result here. The hidden edge-case data has a day when nothing is active: there a WHERE loses the day, COUNT(*) shows 1, and Check fails.
Follow-up. “And on a billion rows?” Do not recompute history for every day on every run.
In production. Keep a daily snapshot table: one row per day and product, written by a job once a day. The report then reads 30 rows instead of scanning transactions 30 times. The same count in Python, in O(n + days), is task P05 on Events and time.
Q06. Customers who came back after a break
Section titled “Q06. Customers who came back after a break”Find customers who came back to the same product after a break of more than 30 days: the next transaction started more than 30 days after the previous one ended.
Output columns: customer_id, product_id, lapsed_on (DATE), returned_on (DATE), gap_daysOrder: ORDER BY returned_on, customer_id, product_id
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
Per product or across products?
Per (customer_id, product_id).
"Previous" by what order?
By start_time, then transaction_id.
How is the gap measured?
Condition: start_time > previous end_time + 30 days. gap_days = returned_on - lapsed_on, both as DATE.
Test?
Excluded.
Trials, promo access, refunds?
Keep all those rows. For a refunded period, still use its end_time.
About the data: transactions
- A customer's history in a product is their rows with the same (customer_id, product_id), in start_time order.
- A break is the gap from one row's end_time to the next row's start_time.
- In the example, chain t_000141 has no breaks: each start_time equals the previous end_time.
A few rows
SELECT transaction_id AS id, original_transaction_id AS chain, is_trial AS trial, is_trial_conversion AS conversion, renewal_number AS n, start_time, end_time, revenue_usd AS net, refunded_atFROM transactionsWHERE original_transaction_id IN ('t_000141', 't_003269')ORDER BY chain, start_time;| id | chain | trial | conversion | n | start_time | end_time | net | refunded_at |
|---|---|---|---|---|---|---|---|---|
| t_000141 | t_000141 | true | false | 1 | 2025-07-06 17:45:04 | 2025-07-13 17:45:04 | 0.00 | NULL |
| t_000142 | t_000141 | false | true | 2 | 2025-07-13 17:45:04 | 2025-08-13 17:45:04 | 4.99 | NULL |
| t_000143 | t_000141 | false | false | 3 | 2025-08-13 17:45:04 | 2025-09-13 17:45:04 | 4.99 | NULL |
| t_000144 | t_000141 | false | false | 4 | 2025-09-13 17:45:04 | 2025-10-13 17:45:04 | 0.00 | 2025-09-20 20:52:05 |
| t_003269 | t_003269 | true | false | 1 | 2026-02-24 11:20:41 | 2026-03-03 11:20:41 | 0.00 | NULL |
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
You need to compare each row with the previous row of the same customer and product. Which window function gives you the previous end_time?
Say it: For one customer, an index on (customer_id, product_id, start_time) returns the rows in order. For the whole table I would check EXPLAIN: a full scan plus a sort is usually cheaper.
Reference solution
WITH ordered AS ( SELECT customer_id, product_id, start_time, LAG(end_time) OVER (PARTITION BY customer_id, product_id ORDER BY start_time, transaction_id) AS prev_end FROM transactions WHERE NOT is_test)SELECT customer_id, product_id, CAST(prev_end AS DATE) AS lapsed_on, CAST(start_time AS DATE) AS returned_on, CAST(start_time AS DATE) - CAST(prev_end AS DATE) AS gap_daysFROM orderedWHERE start_time > prev_end + INTERVAL '30 days'ORDER BY returned_on, customer_id, product_id;Expected output
| customer_id | product_id | lapsed_on | returned_on | gap_days |
|---|---|---|---|---|
| cus_00338 | prd_03 | 2025-02-09 | 2025-05-07 | 87 |
| cus_01112 | prd_17 | 2025-02-28 | 2025-05-08 | 69 |
| cus_02066 | prd_04 | 2025-01-28 | 2025-05-09 | 101 |
| cus_01730 | prd_10 | 2025-03-10 | 2025-05-16 | 67 |
| cus_02162 | prd_17 | 2025-03-10 | 2025-05-31 | 82 |
| cus_02028 | prd_11 | 2025-04-16 | 2025-06-29 | 74 |
| cus_01987 | prd_05 | 2025-05-16 | 2025-07-13 | 58 |
| cus_02254 | prd_05 | 2025-05-19 | 2025-07-23 | 65 |
| cus_00445 | prd_04 | 2025-04-11 | 2025-07-26 | 106 |
| cus_02365 | prd_20 | 2025-06-09 | 2025-08-07 | 59 |
| cus_00841 | prd_03 | 2025-07-11 | 2025-08-10 | 30 |
| cus_01027 | prd_10 | 2025-04-26 | 2025-08-11 | 107 |
| cus_01666 | prd_14 | 2025-06-03 | 2025-08-11 | 69 |
| cus_00908 | prd_06 | 2025-07-04 | 2025-08-17 | 44 |
| cus_00693 | prd_07 | 2025-05-17 | 2025-08-21 | 96 |
| cus_02357 | prd_19 | 2025-06-05 | 2025-08-27 | 83 |
| cus_02104 | prd_05 | 2025-06-13 | 2025-09-03 | 82 |
| cus_01045 | prd_16 | 2025-07-27 | 2025-09-09 | 44 |
| cus_01044 | prd_15 | 2025-08-03 | 2025-09-16 | 44 |
| cus_01232 | prd_12 | 2025-06-12 | 2025-09-18 | 98 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. Win-back: customers who left for more than 30 days and then returned. Marketing uses it to see which products bring people back.
Steps.
- A CTE with
LAG(end_time) OVER (PARTITION BY customer_id, product_id ORDER BY start_time, transaction_id): the end of the previous period of the same customer in the same product. - In the next step,
WHERE start_time > prev_end + INTERVAL '30 days'.
Traps.
- A window function cannot be used in the
WHEREof the sameSELECT. The filter goes into the next step. - The gap in days is the difference of two
DATEvalues,CAST(start_time AS DATE) - CAST(prev_end AS DATE), which is an integer. The difference of two timestamps is aninterval. transaction_idin the window’sORDER BYdecides which row is “previous” when two rows start at the same time. The hidden edge-case data has such customers: a weekly and a monthly plan bought at the same moment, then a return after a break. With the weekly plan first (it has the smaller id), the break runs from the end of the monthly plan: 45 days. In the other order it would run from the end of the weekly plan: 68 days, while the customer still had the monthly plan. Without the tie-breaker the database picks the order by chance, and Check fails there.- The first row of each customer has no previous row, so
prev_endis NULL. The comparison gives NULL, and the row drops out by itself. Mention it, so it does not look like luck.
Check. 121 rows. The first one: cus_00338 in prd_03 lapsed on 2025-02-09 and came back on 2025-05-07, 87 days later.
In production. The window sorts the rows of each (customer_id, product_id) by time. For one customer, an index on (customer_id, product_id, start_time) finds those rows already in that order. For a pass over the whole table it does not help: EXPLAIN here shows a sequential scan and a sort even with that index, because the query reads every row anyway. A covering index with every column the query reads, (customer_id, product_id, start_time, transaction_id) INCLUDE (end_time, is_test), does give an index-only scan with no sort, after VACUUM. Check EXPLAIN before you promise that an index removes a sort.
Q07. Current status of every customer
Section titled “Q07. Current status of every customer”For each (customer, product) take the latest transaction and give the customer a status: 'trial', 'active' or 'expired'. Count customers per product and status.
Output columns: product_id, status, customersOrder: ORDER BY product_id, status
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
Latest by what?
start_time DESC, then transaction_id DESC.
Status rules at today = 2026-09-30 00:00?
Only end_time and refunded_at decide, start_time does not. end_time > today and refunded_at IS NULL: 'trial' if it is a trial, otherwise 'active'. Everything else is 'expired'.
Which transactions do we look at?
Test and promo excluded.
About the data: transactions
- A customer's latest row in a product is the one with the latest start_time among rows with the same (customer_id, product_id).
- The status comes from that row: end_time compared with 'today' (2026-09-30), is_trial, refunded_at.
A few rows
SELECT transaction_id AS id, original_transaction_id AS chain, is_trial AS trial, is_trial_conversion AS conversion, renewal_number AS n, start_time, end_time, revenue_usd AS net, refunded_atFROM transactionsWHERE original_transaction_id IN ('t_000141', 't_003269')ORDER BY chain, start_time;| id | chain | trial | conversion | n | start_time | end_time | net | refunded_at |
|---|---|---|---|---|---|---|---|---|
| t_000141 | t_000141 | true | false | 1 | 2025-07-06 17:45:04 | 2025-07-13 17:45:04 | 0.00 | NULL |
| t_000142 | t_000141 | false | true | 2 | 2025-07-13 17:45:04 | 2025-08-13 17:45:04 | 4.99 | NULL |
| t_000143 | t_000141 | false | false | 3 | 2025-08-13 17:45:04 | 2025-09-13 17:45:04 | 4.99 | NULL |
| t_000144 | t_000141 | false | false | 4 | 2025-09-13 17:45:04 | 2025-10-13 17:45:04 | 0.00 | 2025-09-20 20:52:05 |
| t_003269 | t_003269 | true | false | 1 | 2026-02-24 11:20:41 | 2026-03-03 11:20:41 | 0.00 | NULL |
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
First find each customer's latest row per product, for example with ROW_NUMBER. The status is then a CASE on that one row.
Say it: In production the current status usually lives in its own table, updated from webhooks.
Reference solution
WITH latest AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id, product_id ORDER BY start_time DESC, transaction_id DESC) AS rn FROM transactions WHERE NOT is_test AND channel <> 'promo')SELECT product_id, CASE WHEN end_time > TIMESTAMP '2026-09-30' AND refunded_at IS NULL AND is_trial THEN 'trial' WHEN end_time > TIMESTAMP '2026-09-30' AND refunded_at IS NULL THEN 'active' ELSE 'expired' END AS status, COUNT(*) AS customersFROM latestWHERE rn = 1GROUP BY 1, 2ORDER BY product_id, status;Expected output
| product_id | status | customers |
|---|---|---|
| prd_01 | active | 45 |
| prd_01 | expired | 56 |
| prd_02 | active | 41 |
| prd_02 | expired | 60 |
| prd_03 | active | 58 |
| prd_03 | expired | 58 |
| prd_04 | active | 56 |
| prd_04 | expired | 118 |
| prd_05 | active | 52 |
| prd_05 | expired | 47 |
| prd_06 | active | 44 |
| prd_06 | expired | 57 |
| prd_07 | active | 56 |
| prd_07 | expired | 65 |
| prd_08 | active | 65 |
| prd_08 | expired | 124 |
| prd_08 | trial | 1 |
| prd_09 | active | 42 |
| prd_09 | expired | 65 |
| prd_10 | active | 52 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. A snapshot of who pays now, who is on a trial and who has left, per product. It is the base of a subscriber dashboard.
Steps.
ROW_NUMBER() OVER (PARTITION BY customer_id, product_id ORDER BY start_time DESC, transaction_id DESC);rn = 1is the customer’s latest row.- A
CASEon that row, withend_timeandrefunded_atonly:end_timeafter today and no refund → ‘trial’ or ‘active’; everything else → ‘expired’. GROUP BY product_id, status.
Traps.
- The status comes from the latest row, not from any row of the customer.
- The order of the
CASEbranches matters: ‘trial’ before ‘active’. In the other order, the only trial in the practice data (in prd_08) becomes ‘active’, and the result has 40 rows instead of 41. - Without
transaction_id DESC, two rows with the samestart_timegive a random “latest” row. The hidden edge-case data has such pairs, a trial and a paid month bought at the same moment, so the random pick changes the counts there.
Without a window. MAX(start_time) per (customer, product) in a CTE, then a JOIN back to transactions. It works until two rows share the latest time: then the join returns both, and the customer is counted twice. The practice data has no such pair. Check’s hidden edge-case data has them, so this version fails there.
Check. 41 rows: 20 products × (‘active’, ‘expired’), plus one ‘trial’. In total there are 2490 customer–product pairs: 1044 active, 1445 expired and 1 trial.
Follow-up. “What about 50 million customers?” Keep a table of current statuses, one row per customer and product, updated from webhooks with INSERT … ON CONFLICT DO UPDATE.
In production. The real answer is the table of current statuses from the follow-up: the service should not compute a status from history every time. An index on (customer_id, product_id, start_time DESC) finds one customer’s latest row quickly. For the whole table it does not remove the sort: the query reads every row and every column, and EXPLAIN here shows a sequential scan and a sort even with that index.
Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.