Monthly Channel Cohorts: Measuring First-Month Account Usage
SQL & DataModel answerLast updated
Write a SQL report of how many new banking users make three or more completed debits totaling at least $50 in their first 30 days, grouped by signup month and channel, excluding immature cohorts.
Context
A digital bank wants to know how well each marketing channel brings in users who actually use their accounts. A user who opens an account but never spends contributes little value, so the team tracks a short-term "first-month usage" measure.
A user counts as activated if, within the first 30 days after signup (day 0 through day 30, both inclusive), they have:
- at least 3 qualifying transactions, and
- those transactions sum to at least $50.00.
A qualifying transaction has transaction_type = 'debit' and status = 'completed'.
Schema
users
| Column | Type | Description |
|---|---|---|
user_id | INT | Primary key |
signup_date | DATE | Day the account was opened |
acquisition_channel | VARCHAR | Marketing channel, e.g. 'organic', 'paid_search', 'referral' |
transactions
| Column | Type | Description |
|---|---|---|
transaction_id | INT | Primary key |
user_id | INT | References users.user_id |
transaction_date | DATE | Day of the transaction |
transaction_type | VARCHAR | 'debit', 'deposit' or 'transfer' |
status | VARCHAR | 'completed', 'pending', 'declined' or 'reversed' |
amount | DECIMAL(10, 2) | Positive USD amount |
Task
Write a SQL query (PostgreSQL syntax is fine) that returns one row per signup month and acquisition channel with these columns:
cohort_month: first day of the signup month as text'YYYY-MM-01'.acquisition_channeltotal_users: users in that month and channel.activated_users: users from that group who meet the activation rule.activation_rate_pct:activated_users * 100.0 / total_users, rounded to 2 decimals.
Rules
- Count only transactions with
status = 'completed'andtransaction_type = 'debit'. - A transaction counts only if its date falls between
signup_dateandsignup_date + 30 days, inclusive. - Leave out users who signed up within the last 30 days relative to
CURRENT_DATE, since their 30-day window is not yet fully observed. - Sort by
cohort_monthascending, thenacquisition_channelascending.
Sample output
cohort_month | acquisition_channel | total_users | activated_users | activation_rate_pct
-------------+---------------------+-------------+-----------------+--------------------
2023-01-01 | organic | 1200 | 480 | 40.00
2023-01-01 | paid_search | 2500 | 750 | 30.00
2023-01-01 | referral | 800 | 440 | 55.00
2023-02-01 | organic | 1350 | 567 | 42.00
What to cover
Give the query, explain why it is correct, discuss edge cases, and state any assumptions (for example, how you interpret the maturity cutoff).
Hints
Hint 1
Split the work into stages: first pick the eligible users, then compute per-user transaction counts and totals inside the window, then aggregate by month and channel.
Hint 2
Users with no qualifying transactions must still appear in `total_users`. Think about which table should drive the join and which join type keeps them.
Hint 3
Aggregate transactions to one row per user before joining back, so the final `COUNT(*)` counts users and not transactions. Use conditional aggregation (`FILTER` or `CASE`) for the numerator.
Hint 4
Decide whether the maturity cutoff should be strict (`<`) or not, and justify it: a user whose day 30 is today has an unfinished window.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a cohort rollup with a per-user condition. I split it into three steps.
- Eligible users. Keep users whose whole 30-day window is already in the past, and compute
cohort_monthfromsignup_date. - Per-user activity. For each eligible user, count and sum completed debits dated within
[signup_date, signup_date + 30 days]. - Cohort rollup. Group by cohort month and channel. The denominator is all eligible users; the numerator is users with at least 3 transactions and at least $50.00 in total.
The classic mistake is dropping users who never transacted. They belong in the denominator, so the per-user activity must be LEFT JOINed onto the user list, not the other way round.
Query (PostgreSQL)
WITH eligible_users AS (
SELECT
user_id,
signup_date,
acquisition_channel,
DATE_TRUNC('month', signup_date)::date AS cohort_month
FROM users
WHERE signup_date < CURRENT_DATE - INTERVAL '30 days'
),
user_activity AS (
SELECT
u.user_id,
COUNT(*) AS n_debits,
SUM(t.amount) AS debit_spend
FROM eligible_users u
JOIN transactions t
ON t.user_id = u.user_id
AND t.transaction_type = 'debit'
AND t.status = 'completed'
AND t.transaction_date >= u.signup_date
AND t.transaction_date <= u.signup_date + INTERVAL '30 days'
GROUP BY u.user_id
)
SELECT
TO_CHAR(e.cohort_month, 'YYYY-MM-01') AS cohort_month,
e.acquisition_channel,
COUNT(*) AS total_users,
COUNT(*) FILTER (WHERE a.n_debits >= 3 AND a.debit_spend >= 50.00) AS activated_users,
ROUND(
COUNT(*) FILTER (WHERE a.n_debits >= 3 AND a.debit_spend >= 50.00) * 100.0
/ COUNT(*), 2
) AS activation_rate_pct
FROM eligible_users e
LEFT JOIN user_activity a ON a.user_id = e.user_id
GROUP BY e.cohort_month, e.acquisition_channel
ORDER BY e.cohort_month ASC, e.acquisition_channel ASC;
On engines without FILTER, use SUM(CASE WHEN a.n_debits >= 3 AND a.debit_spend >= 50 THEN 1 ELSE 0 END).
Why it works
- Complete denominator.
eligible_usersdrives the LEFT JOIN, so each mature user is counted exactly once. Users with no qualifying debits get NULLs;NULL >= 3is unknown, so they fall out of the numerator, which is correct. - No row multiplication.
user_activityis already one row per user, soCOUNT(*)in the final step counts users. If you join raw transactions instead, you must useCOUNT(DISTINCT user_id)and conditional logic at user level, otherwise the totals inflate. - Filter placement. The type, status and date conditions are in the
ONclause of an inner join inside the CTE, which is fine. In a single-pass LEFT JOIN design, moving them intoWHEREwould turn it into an inner join and silently drop inactive users. - Inclusive boundaries.
>= 3,>= 50.00, day 0 and day 30 all follow "at least" and "inclusive". - Maturity cutoff. A user who signed up exactly 30 days ago is on day 30 today, and today is still incomplete. The strict
<therefore excludes them. If an interviewer reads the rule as<=, say so out loud; the important thing is to state the assumption.
Complexity
With U users and T transactions, hash joins and hash aggregation give roughly O(T + U) time and O(U) space for per-user aggregates (plus a small number of output groups). An index on transactions(user_id, transaction_date) makes the per-user window lookup a cheap range scan.
Edge cases
- Users with zero transactions: counted in
total_users, never activated. - Pending, declined or reversed rows: excluded by the status filter. If reversals are stored as separate rows rather than overwriting status, net them out.
- Transactions before signup: excluded by the lower bound and probably a data-quality issue worth flagging.
- Exact thresholds: a transaction on day 30 counts, and so does a total of exactly $50.00. Use DECIMAL, never floats.
- Newest cohort month: because of the maturity filter, the latest month may contain only its earliest signups. Flag it or drop partial months.
- NULL or messy channel values: consider
COALESCE(acquisition_channel, 'unknown')and normalizing case. - Integer division: multiplying by
100.0forces numeric division. A group exists only if it has users, so the denominator is never zero. - Time zones: if dates are derived from timestamps, decide between UTC and user-local day boundaries.
Common mistakes
- Using an inner join from users to transactions and losing inactive users.
- Checking the count and the sum with separate subqueries that can disagree on the window.
- Counting transactions rather than users in the denominator.
- Forgetting the maturity filter and understating recent cohorts.
Follow-ups worth preparing
- Early read on young cohorts. Use a shorter horizon such as 7-day activation as a leading indicator, validated against the 30-day measure. Alternatively, plot cumulative activation by days since signup so cohorts are compared at equal age, or model the 30-day rate with a time-to-event approach that treats young users as censored, and report it with uncertainty.
- Is a channel causing better activation or just attracting better users? The cleanest answer is a randomized experiment, for example randomizing the referral incentive or prompt, and analyzing by intent to treat. When that is not possible, use geo holdouts, or observational methods such as matching or reweighting on pre-signup covariates, while being explicit that unobserved self-selection may remain.