Feature Activation, Time-to-First-Purchase, and Region Switching in SQL
SQL & DataModel answerLast updated
Write SQL over users and transactions for regional activation and purchase rates, first-purchase latency with percentiles, and flagging purchases made outside a user's first-purchase region.
A payments app launched a new wallet feature. You have two tables.
users(user_id INT PRIMARY KEY, user_region STRING, adopted_at TIMESTAMP NULL)
transactions(txn_id INT PRIMARY KEY, user_id INT, txn_ts TIMESTAMP, txn_region STRING)
adopted_at is the moment a user turned the feature on, or NULL if they never did.
users
| user_id | user_region | adopted_at |
|---|---|---|
| 1 | US | 2025-07-15 10:00:00 |
| 2 | US | null |
| 3 | EU | 2025-08-02 09:12:00 |
| 4 | APAC | 2025-08-20 18:30:00 |
| 5 | EU | 2025-08-25 12:00:00 |
| 6 | US | 2025-08-30 23:59:00 |
transactions
| txn_id | user_id | txn_ts | txn_region |
|---|---|---|---|
| 101 | 1 | 2025-08-01 08:00:00 | US |
| 102 | 1 | 2025-08-16 10:00:00 | CA |
| 103 | 3 | 2025-08-10 12:10:00 | EU |
| 104 | 4 | 2025-08-25 07:45:00 | APAC |
| 105 | 4 | 2025-08-26 07:45:00 | US |
| 106 | 5 | 2025-08-30 07:45:00 | EU |
| 107 | 6 | 2025-09-01 00:10:00 | US |
Write ANSI-style SQL for each part. "August" means 2025-08-01 through 2025-08-31 inclusive, covering the whole of the 31st.
A. Regional rates. For each user_region, return region, adoption_rate and transaction_rate.
adoption_rateis the number of users whoseadopted_atfalls in August, divided by all users in that region.transaction_rateis the number of distinct users in that region with at least one transaction in August, divided by all users in that region.- Attribute by the user's region, not by
txn_region.
B. Time to first transaction.
- For each user, compute
days_to_first_txn, the number of days fromadopted_atto the user's earliest transaction. Include only users with a non-nulladopted_atwhose earliest transaction is at or afteradopted_at. Returnuser_idanddays_to_first_txn. - Write a second query returning the p10, p50 and p90 of
days_to_first_txn.
C. Cross-region transactions. A user's first_txn_region is the region of their earliest transaction ever, not just in August. A transaction is cross-region if its txn_region differs from that value. Return every transaction dated in August, with columns txn_id, user_id, txn_ts, txn_region, first_txn_region and is_cross_region.
Explain your choices on date boundaries, day-difference semantics, percentile type and tie-breaking.
Hints
Hint 1
Both rates share the same denominator, the user count per region. Shrink the transactions table to one row per user (DISTINCT user_id within August) before joining, so the join cannot multiply user rows.
Hint 2
Avoid BETWEEN on timestamps with '2025-08-31' as the upper bound, since it cuts off the last day. Use a half-open range: >= '2025-08-01' and < '2025-09-01'.
Hint 3
For part C, compute each user's first region with ROW_NUMBER() over all of their transactions, ordered by txn_ts then txn_id. Apply the August filter only afterwards, in the outer query.
Hint 4
For percentiles, build the per-user latency as a CTE and run PERCENTILE_CONT ... WITHIN GROUP over it. Decide whether you want interpolated or observed values, and say which you chose.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
All three parts follow one pattern: reduce data to a grain where each entity appears once, then join or aggregate. The main traps are:
- Timestamp boundaries.
BETWEEN '2025-08-01' AND '2025-08-31'stops at midnight on the 31st and loses most of that day. Use>= 2025-08-01and< 2025-09-01. - Join fan-out. A user with many transactions would be counted many times if you joined raw rows. Collapse to one row per user first.
- Filter-before-window. "First region ever" must be computed over the full history. Filtering to August first would give wrong answers for users whose first transaction was earlier.
- Integer division. Cast to a decimal type before dividing, or you get 0 or 1.
Part A: regional rates
Build a CTE of distinct users with an August transaction, then left-join it from users. The join is one-to-one, so COUNT(*) is the true regional user count.
WITH aug_buyers AS (
SELECT DISTINCT user_id
FROM transactions
WHERE txn_ts >= TIMESTAMP '2025-08-01 00:00:00'
AND txn_ts < TIMESTAMP '2025-09-01 00:00:00'
)
SELECT
u.user_region AS region,
CAST(SUM(CASE WHEN u.adopted_at >= TIMESTAMP '2025-08-01 00:00:00'
AND u.adopted_at < TIMESTAMP '2025-09-01 00:00:00'
THEN 1 ELSE 0 END) AS DECIMAL(10,4)) / COUNT(*) AS adoption_rate,
CAST(COUNT(b.user_id) AS DECIMAL(10,4)) / COUNT(*) AS transaction_rate
FROM users u
LEFT JOIN aug_buyers b ON b.user_id = u.user_id
GROUP BY u.user_region;
A NULL adopted_at makes the comparison unknown, so the CASE falls to ELSE 0. The user stays in the denominator but not the numerator.
Expected result:
| region | adoption_rate | transaction_rate |
|---|---|---|
| US | 0.3333 | 0.3333 |
| EU | 1.0000 | 1.0000 |
| APAC | 1.0000 | 1.0000 |
For US, only user 6 adopted in August (at 23:59 on the 30th), and only user 1 transacted in August. User 6's only transaction is on Sep 1. User 4 counts toward APAC even though one of their transactions was in the US.
Part B: days to first transaction
Take each user's lifetime MIN(txn_ts), join to users with a non-null adopted_at, and keep rows where the first transaction is not before adoption. Users whose first-ever transaction predates adoption are dropped, not remapped to a later transaction. That follows the stated rule, but confirm it.
ANSI SQL has no portable DATE_DIFF, so state the semantics. I use calendar-day difference (cast both to date):
WITH first_txn AS (
SELECT user_id, MIN(txn_ts) AS first_txn_ts
FROM transactions
GROUP BY user_id
),
latency AS (
SELECT u.user_id,
CAST(f.first_txn_ts AS DATE) - CAST(u.adopted_at AS DATE) AS days_to_first_txn
FROM users u
JOIN first_txn f ON f.user_id = u.user_id
WHERE u.adopted_at IS NOT NULL
AND f.first_txn_ts >= u.adopted_at
)
SELECT user_id, days_to_first_txn FROM latency ORDER BY user_id;
date - date returns an integer in Postgres. Elsewhere use DATE_DIFF(..., DAY) or DATEDIFF.
Result: user 1 → 17, user 3 → 8, user 4 → 5, user 5 → 5, user 6 → 2. User 2 is excluded (null adoption, no transactions).
If the interviewer wants elapsed 24-hour periods (floor of the time difference), the values become 16, 8, 4, 4, 1. Say which one you are using.
Percentiles reuse the latency CTE:
SELECT
PERCENTILE_CONT(0.1) WITHIN GROUP (ORDER BY days_to_first_txn) AS p10,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY days_to_first_txn) AS p50,
PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY days_to_first_txn) AS p90
FROM latency;
Sorted values are {2, 5, 5, 8, 17}. PERCENTILE_CONT interpolates at position 1 + p(n-1):
- p10: position 1.4, so 2 + 0.4 × 3 = 3.2
- p50: 5
- p90: position 4.6, so 8 + 0.6 × 9 = 13.4
PERCENTILE_DISC returns observed values instead (2, 5, 17). With five data points, the interpolated numbers are values no user actually had. Some engines (MySQL, older SQL Server) lack ordered-set aggregates. There you can use NTILE, PERCENT_RANK, or an approx_percentile function.
Part C: cross-region flag
Rank each user's transactions over their entire history, take rank 1 as the first region, join back, and filter to August last.
WITH ranked AS (
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY txn_ts, txn_id) AS rn
FROM transactions t
),
first_region AS (
SELECT user_id, txn_region AS first_txn_region
FROM ranked
WHERE rn = 1
)
SELECT t.txn_id, t.user_id, t.txn_ts, t.txn_region, f.first_txn_region,
CASE WHEN t.txn_region IS DISTINCT FROM f.first_txn_region
THEN 1 ELSE 0 END AS is_cross_region
FROM transactions t
JOIN first_region f ON f.user_id = t.user_id
WHERE t.txn_ts >= TIMESTAMP '2025-08-01 00:00:00'
AND t.txn_ts < TIMESTAMP '2025-09-01 00:00:00'
ORDER BY t.txn_ts;
Adding txn_id to the window ordering makes ties on identical timestamps deterministic. IS DISTINCT FROM is null-safe. A plain <> returns unknown when a region is null, and the CASE would silently report 0.
Expected rows (107 is excluded as it falls on Sep 1):
| txn_id | user_id | txn_region | first_txn_region | is_cross_region |
|---|---|---|---|---|
| 101 | 1 | US | US | 0 |
| 102 | 1 | CA | US | 1 |
| 103 | 3 | EU | EU | 0 |
| 104 | 4 | APAC | APAC | 0 |
| 105 | 4 | US | APAC | 1 |
| 106 | 5 | EU | EU | 0 |
If the dialect lacks IS DISTINCT FROM, use COALESCE on both sides with a sentinel, or an explicit null check.
Complexity
Parts A and B are O(U + T) with hash aggregation and O(U) extra space. Part C needs a partitioned sort, so O(T log T) time and O(T) space. An index on transactions(user_id, txn_ts) helps both the MIN and the window.
Common mistakes
- Using
BETWEENwith a date upper bound on a timestamp column. - Joining users to raw transactions and then using
COUNT(*), which inflates denominators. - Filtering to August before computing the first region.
- Attributing regions by
txn_regionin part A. - Forgetting that users with no transactions or null adoption still belong in part A's denominators.
- Integer division producing 0.
Edge cases to raise
- Time zone definition of "August" (UTC versus local). User 6's 23:59 adoption is sensitive to it.
- Transactions before adoption, which may indicate a data quality issue.
- Duplicate timestamps for a user's first transaction.
- Null regions and null-safe comparison.
- Regions with zero users never appear, since the output is driven by
users.