SQL Quest › SQL Interview Questions › Window Functions
Peak Rolling 30-Day Spend Per Account
Credit-limit and velocity models both start here: the most a card spent in any 30-day window. For every transaction, sum the account's spend over the 30 days ending at that transaction, then take each account's maximum. The frame is the whole question: SUM(amount) OVER (PARTITION BY account_id ORDER BY julianday(txn_at) RANGE BETWEEN 30 PRECEDING AND CURRENT ROW) — ordering by julianday() turns the timestamp into a number of days, so a RANGE frame of 30 means 30 days, not 30 rows. ROWS BETWEEN 30 PRECEDING is the classic wrong answer here.
Compute the rolling sum in a CTE, then return account_id and peak_30d_spend (MAX of the rolling sum, rounded to 2 decimals) for the 20 accounts with the highest peak. Order by peak_30d_spend descending, then account_id ascending.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
transactions
| txn_id | account_id | amount | txn_at | merchant_id | lat | lng | status |
|---|---|---|---|---|---|---|---|
| 1 | 149 | 151.84 | 2026-03-04T12:39:39.078Z | 24 | 40.342 | -74.4092 | completed |
| 2 | 21 | 10.72 | 2026-03-04T13:04:50.641Z | 7 | 52.9366 | 13.3229 | completed |
| 3 | 35 | 158.78 | 2026-03-04T13:27:15.528Z | 25 | 52.7397 | 13.3134 | completed |
Expected output: account 42 → 10427.13, account 150 → 9207.71 ...
Hint
Concepts
SELECT Window Functions Frame Clause CTE JULIANDAY Window Functions + CTE
Practise the topic: SQL practice questions · Window function practice · CTE practice · Advanced SQL interview questions
Read the concept: ROW_NUMBER vs RANK vs DENSE_RANK
In these company practice sets
Capital One · Revolut · Bloomberg
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Preparing for Capital One? How the CodeSignal data analyst assessment works.
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test