Invented schema:
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)
Sample rows:
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 SQL for the following:
A) Adoption_rate and transaction_rate per user_region for the month 2025-08-01 to 2025-08-31 (inclusive). Definitions: adoption_rate = users with adopted_at in August 2025 divided by total users in that region; transaction_rate = distinct users in that region having ≥1 transaction with txn_ts in August 2025 divided by total users in that region. Return region, adoption_rate, transaction_rate.
B) For each user, compute days_to_first_txn = DATE_DIFF(first transaction timestamp, adopted_at) in days. Only include users with non-null adopted_at and first transaction timestamp ≥ adopted_at. Return user_id and days_to_first_txn, and also a separate query that returns the p10, p50, p90 of days_to_first_txn.
C) Define a cross-region sale as any transaction where txn_region ≠ the user’s first_txn_region (the region of the earliest transaction in the user’s lifetime, not limited to August). Return all transactions with txn_ts in 2025-08-01..2025-08-31 flagged with is_cross_region plus columns: txn_id, user_id, txn_ts, txn_region, first_txn_region.