Conversion and churn
Product metrics come next: the share of trials that converted, a daily revenue line with a running total and a 7-day average, and the share of subscriptions that were not renewed. Each is a few CTEs, a rate without integer division, and a clear answer to “who is in the denominator?”. They build on CTE and EXISTS, CASE, FILTER and rates and Shares and running totals.
Q08. Trial conversion by plan — Q2 2026
Section titled “Q08. Trial conversion by plan — Q2 2026”For trials that started in April–June 2026, show per plan how many trials there were, how many converted to paid, and the conversion rate.
Output columns: plan_id, trials, conversions, conversion_rateOrder: ORDER BY plan_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.
How do I know a trial converted?
The same chain (original_transaction_id) has a transaction with is_trial_conversion = true.
Rate format?
conversions / trials, rounded to 3 decimals. Watch out for integer division in PostgreSQL.
Test?
Excluded everywhere.
A trial started in late June and converted in July. Does it count?
Yes. The start of the trial decides the quarter; the conversion can come later.
About the data: transactions
- All periods of one subscription form a chain: they share original_transaction_id (for the first row it equals its own id).
- A trial is a row with is_trial = true. If the customer paid after the trial, the same chain has a row with is_trial_conversion = true.
- In the example, chain t_000141 is a trial that converted (then a renewal and a refund); t_003269 is a trial that never converted — one row.
- A product has several plans (plan_id, for example prd_02.monthly and prd_02.annual): the result has one row per plan.
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
A trial and its conversion are different rows of one chain. Put the Q2 trials in one CTE and the converted chains in another, then choose the join that keeps trials without a conversion.
Say it: Trials and conversions are linked by original_transaction_id, so I would index that column.
Reference solution
WITH trials AS ( SELECT plan_id, original_transaction_id FROM transactions WHERE is_trial AND NOT is_test AND start_time >= TIMESTAMP '2026-04-01' AND start_time < TIMESTAMP '2026-07-01'),conversions AS ( SELECT DISTINCT original_transaction_id FROM transactions WHERE is_trial_conversion AND NOT is_test)SELECT t.plan_id, COUNT(*) AS trials, COUNT(c.original_transaction_id) AS conversions, ROUND(1.0 * COUNT(c.original_transaction_id) / COUNT(*), 3) AS conversion_rateFROM trials tLEFT JOIN conversions c USING (original_transaction_id)GROUP BY t.plan_idORDER BY t.plan_id;Expected output
| plan_id | trials | conversions | conversion_rate |
|---|---|---|---|
| prd_01.annual | 2 | 0 | 0.000 |
| prd_01.monthly | 2 | 0 | 0.000 |
| prd_02.annual | 3 | 0 | 0.000 |
| prd_02.monthly | 5 | 2 | 0.400 |
| prd_03.annual | 4 | 1 | 0.250 |
| prd_03.monthly | 2 | 2 | 1.000 |
| prd_04.annual | 2 | 1 | 0.500 |
| prd_04.monthly | 3 | 1 | 0.333 |
| prd_05.annual | 3 | 1 | 0.333 |
| prd_05.monthly | 5 | 4 | 0.800 |
| prd_06.annual | 2 | 1 | 0.500 |
| prd_06.monthly | 4 | 1 | 0.250 |
| prd_07.annual | 4 | 1 | 0.250 |
| prd_07.monthly | 8 | 3 | 0.375 |
| prd_08.annual | 3 | 3 | 1.000 |
| prd_08.monthly | 3 | 2 | 0.667 |
| prd_09.annual | 2 | 0 | 0.000 |
| prd_09.monthly | 2 | 0 | 0.000 |
| prd_10.annual | 4 | 3 | 0.750 |
| prd_10.monthly | 3 | 3 | 1.000 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. The share of free trials that turned into a payment: the main number of a trial funnel. The trial and the first payment are different rows of one chain, with the same original_transaction_id.
Steps.
- A CTE
trials: trials that started in April–June. - A CTE
conversions: the chains that have a row withis_trial_conversion. trials LEFT JOIN conversions:COUNT(*)is the number of trials,COUNT(c.original_transaction_id)the number of conversions, and the rate is1.0 * conversions / trials.
Traps.
- An
INNER JOINloses the trials that did not convert, and every rate becomes 1. - Integer division gives 0, or 1 when every trial converted.
- Each chain here has at most one conversion row.
DISTINCTinconversionskeeps the count right if a chain ever has two. - A trial can convert in July. The start of the trial decides the quarter, so do not filter conversions by date. Here 3 trials converted in July.
Check. 39 rows, one per plan. Overall there are 139 trials and 63 conversions, a rate of 0.453. prd_02.monthly: 5 trials, 2 conversions, 0.400.
Follow-up. “Can we trust the number for June?” A trial lasts 7 days, so trials from the last week before “today” have no outcome yet. For Q2 that week is long past, but for a report on the last month it matters: mark that period as incomplete. More follow-ups of this kind are on Database concepts.
In production. A trial reaches its conversion through original_transaction_id, so index that column. It can be a partial index on the side the query looks up: WHERE is_trial_conversion.
Q09. Running revenue and 7-day average per merchant
Section titled “Q09. Running revenue and 7-day average per merchant”For every merchant and every day from 2026-09-01 to 2026-09-29, show the net revenue of that day, the running total since the start of the month, and the average of the last 7 days.
Output columns: merchant_id, day (DATE), net_usd, running_usd, avg_7d_usdOrder: ORDER BY merchant_id, 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.
Days without revenue?
Must appear with 0 and count in the average.
7-day average of what window?
The current day and the 6 days before it. For the first days of the month, average over the days that exist (no August data). Round to 2 decimals.
Net revenue?
SUM(revenue_usd) by the day of start_time, test purchases excluded.
About the data: transactions, products, merchants
- Revenue per day is SUM(revenue_usd) by CAST(start_time AS DATE), test purchases excluded.
- Every merchant × every day comes from a calendar and a CROSS JOIN; empty days are 0.
"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
Before any window function, build a grid of merchants and days, so that every day has a row. The running total and the 7-day average are then two windows with different frames.
Say it: The window part is cheap; the cost is reading transactions, so an index on start_time.
Reference solution
WITH days AS ( SELECT CAST(d AS DATE) AS day FROM generate_series(TIMESTAMP '2026-09-01', TIMESTAMP '2026-09-29', INTERVAL '1 day') AS g(d)),daily AS ( SELECT p.merchant_id, CAST(t.start_time AS DATE) AS day, SUM(t.revenue_usd) AS net_usd FROM transactions t JOIN products p ON p.id = t.product_id WHERE NOT t.is_test AND t.start_time >= TIMESTAMP '2026-09-01' AND t.start_time < TIMESTAMP '2026-09-30' GROUP BY 1, 2),grid AS ( SELECT m.id AS merchant_id, days.day, COALESCE(daily.net_usd, 0) AS net_usd FROM merchants m CROSS JOIN days LEFT JOIN daily ON daily.merchant_id = m.id AND daily.day = days.day)SELECT merchant_id, day, net_usd, SUM(net_usd) OVER (PARTITION BY merchant_id ORDER BY day) AS running_usd, ROUND(AVG(net_usd) OVER (PARTITION BY merchant_id ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), 2) AS avg_7d_usdFROM gridORDER BY merchant_id, day;Expected output
| merchant_id | day | net_usd | running_usd | avg_7d_usd |
|---|---|---|---|---|
| mer_01 | 2026-09-01 | 6.99 | 6.99 | 6.99 |
| mer_01 | 2026-09-02 | 0.00 | 6.99 | 3.50 |
| mer_01 | 2026-09-03 | 13.98 | 20.97 | 6.99 |
| mer_01 | 2026-09-04 | 6.99 | 27.96 | 6.99 |
| mer_01 | 2026-09-05 | 0 | 27.96 | 5.59 |
| mer_01 | 2026-09-06 | 0 | 27.96 | 4.66 |
| mer_01 | 2026-09-07 | 0 | 27.96 | 3.99 |
| mer_01 | 2026-09-08 | 0 | 27.96 | 3.00 |
| mer_01 | 2026-09-09 | 6.99 | 34.95 | 3.99 |
| mer_01 | 2026-09-10 | 0 | 34.95 | 2.00 |
| mer_01 | 2026-09-11 | 93.96 | 128.91 | 14.42 |
| mer_01 | 2026-09-12 | 6.99 | 135.90 | 15.42 |
| mer_01 | 2026-09-13 | 0 | 135.90 | 15.42 |
| mer_01 | 2026-09-14 | 0 | 135.90 | 15.42 |
| mer_01 | 2026-09-15 | 6.99 | 142.89 | 16.42 |
| mer_01 | 2026-09-16 | 0 | 142.89 | 15.42 |
| mer_01 | 2026-09-17 | 6.99 | 149.88 | 16.42 |
| mer_01 | 2026-09-18 | 6.99 | 156.87 | 3.99 |
| mer_01 | 2026-09-19 | 0 | 156.87 | 3.00 |
| mer_01 | 2026-09-20 | 0 | 156.87 | 3.00 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. Daily revenue of each merchant with a running total and a 7-day average: a typical dashboard chart.
Steps.
daily: net revenue per merchant and day.- A grid:
merchants CROSS JOIN days, thenLEFT JOIN dailywithCOALESCE(net_usd, 0). - Windows over the grid:
SUM(net_usd) OVER (PARTITION BY merchant_id ORDER BY day)for the running total, andAVG(net_usd) OVER (… ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)for the average.
Traps.
- Without the grid, empty days drop out, and “7 rows” stop being 7 days. Only 222 merchant-days have sales; the grid has 348.
AVGwithout a frame uses the default one, from the start of the partition to the current row. That is a running average, not a 7-day one. For mer_01 on 8 September the default frame gives 3.50; the 7-day average is 3.00.ROWScounts rows, so it is right only when there is exactly one row per day. The grid guarantees that.
Check. 348 rows: 12 merchants × 29 days. mer_01 has 6.99 on 1 September and a net of 0 on 2 September, so the average on the 2nd is 3.50: over the two days that exist.
In production. The windows work on a small set, days × merchants. The expensive part is reading transactions, so an index on start_time.
Q10. Churn rate per product — August 2026
Section titled “Q10. Churn rate per product — August 2026”Take the paid subscriptions that were active on August 1, 2026. How many of them ended during August and were not renewed (churned)? Show it per product: active on August 1, churned, churn rate. Worst products first.
Output columns: product_id, active_at_start, churned, churn_rateOrder: ORDER BY churn_rate 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 is one subscription?
All its paid periods: the first purchase and every renewal share the same original_transaction_id. Use only paid rows: not trial, not refunded, not a test purchase, not promo.
Active on August 1?
The first start_time of the subscription <= 2026-08-01 and the last end_time of the subscription > 2026-08-01.
Churned in August?
Active on August 1 and no longer active on 2026-09-01 00:00: the last end_time of the subscription <= 2026-09-01.
Rate?
churned / active_at_start, rounded to 4 decimals.
Do you also need every month of the year?
Not now. Say how you would extend it: a calendar of months instead of one date.
About the data: transactions
- One subscription = all its rows with the same original_transaction_id: the first purchase and every renewal. Each row is one paid period, start_time to end_time.
- So the subscription lives from the earliest start_time to the latest end_time of its paid rows. If it was active on 1 August and is no longer active on 1 September 00:00, nobody renewed it — that is churn.
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
Collapse each subscription to one row first: group the paid rows by original_transaction_id and take the first start and the last end. Then active on 1 August and churned are simple conditions.
Say it: This groups the whole table, so an index does not help. In production I would keep one row per subscription with its first start and last end.
Reference solution
WITH chains AS ( SELECT original_transaction_id AS chain_id, product_id, MIN(start_time) AS first_start, MAX(end_time) AS last_end FROM transactions WHERE NOT is_trial AND NOT is_test AND channel <> 'promo' AND refunded_at IS NULL GROUP BY original_transaction_id, product_id),active AS ( SELECT * FROM chains WHERE first_start <= TIMESTAMP '2026-08-01' AND last_end > TIMESTAMP '2026-08-01')SELECT product_id, COUNT(*) AS active_at_start, COUNT(*) FILTER (WHERE last_end <= TIMESTAMP '2026-09-01') AS churned, ROUND(1.0 * COUNT(*) FILTER (WHERE last_end <= TIMESTAMP '2026-09-01') / COUNT(*), 4) AS churn_rateFROM activeGROUP BY product_idORDER BY churn_rate DESC, product_id;Expected output
| product_id | active_at_start | churned | churn_rate |
|---|---|---|---|
| prd_08 | 60 | 7 | 0.1167 |
| prd_10 | 54 | 5 | 0.0926 |
| prd_17 | 46 | 4 | 0.0870 |
| prd_05 | 49 | 4 | 0.0816 |
| prd_11 | 53 | 4 | 0.0755 |
| prd_07 | 56 | 4 | 0.0714 |
| prd_13 | 50 | 3 | 0.0600 |
| prd_18 | 52 | 3 | 0.0577 |
| prd_20 | 62 | 3 | 0.0484 |
| prd_01 | 44 | 2 | 0.0455 |
| prd_02 | 44 | 2 | 0.0455 |
| prd_16 | 46 | 2 | 0.0435 |
| prd_19 | 46 | 2 | 0.0435 |
| prd_12 | 50 | 2 | 0.0400 |
| prd_14 | 54 | 2 | 0.0370 |
| prd_09 | 40 | 1 | 0.0250 |
| prd_06 | 44 | 1 | 0.0227 |
| prd_03 | 56 | 1 | 0.0179 |
| prd_04 | 47 | 0 | 0.0000 |
| prd_15 | 40 | 0 | 0.0000 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. Churn: of the subscriptions that were paid on 1 August, how many were not renewed in August. One subscription is all its rows with one original_transaction_id. It lives from the first start_time to the last end_time of its paid rows.
Steps.
- A CTE
chains: paid rows only,GROUP BY original_transaction_id, product_id→MIN(start_time)andMAX(end_time). - A CTE
active: chains alive on 1 August. - The result:
GROUP BY product_id→COUNT(*),COUNT(*) FILTER (WHERE last_end <= '2026-09-01')and the rate.
Traps.
MAX(start_time)instead ofMAX(end_time). The end of a subscription is the end of its last period.last_end < '2026-09-01'misses a subscription that ends exactly at 1 September 00:00. It is active on 1 August and no longer active on 1 September, so it churned in August; with<it never counts as churn in any month. No practice chain ends exactly at that midnight. The hidden edge-case data has one, so<fails there.refunded_at IS NOT NULLinstead ofIS NULLkeeps only the refunds.- Forgotten trials move the start of a subscription 7 days earlier. For example, a subscription paid from 3 August after a trial from 27 July would look active on 1 August.
- The rate needs
1.0 *; integer division gives 0.
Check. The worst product is prd_08: 7 of 60, 0.1167. In total, 993 subscriptions were active on 1 August and 52 churned.
Follow-up.
- “And for every month of the year?” A calendar of months instead of one date:
generate_seriesand a grid. Answer with the idea. - “Why is prd_08 the worst?” Look at the plans. Annual subscriptions rarely end in a given month: 14 of 696 churned in August, against 31 of 284 monthly and 7 of 13 weekly ones. So churn per plan differs much more than churn per product. prd_08 has 40 annual subscriptions out of 60; its 7 losses are 3 monthly, 3 weekly and 1 annual.
In production. The query groups the whole table, so an index does not make it cheaper: EXPLAIN shows a hash aggregate over a sequential scan, with or without an index on (original_transaction_id, start_time). Two things help. Keep a table with one row per subscription (first_start, last_end), updated with every renewal: churn for any month is then a pass over that small table. Or first pick the chains that have a period ending after 1 August (an index on end_time finds them) and group only those. On this small table one full pass is still cheaper, as EXPLAIN shows, so check it on the real one.
Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.