SQL QuestSQL Interview Questions › Window Functions

Peak Rolling 30-Day Spend Per Account

HardProQuerying BasicsWindow FunctionsSubqueries & CTEs

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_idaccount_idamounttxn_atmerchant_idlatlngstatus
1149151.842026-03-04T12:39:39.078Z2440.342-74.4092completed
22110.722026-03-04T13:04:50.641Z752.936613.3229completed
335158.782026-03-04T13:27:15.528Z2552.739713.3134completed

Expected output: account 42 → 10427.13, account 150 → 9207.71 ...

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

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

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.

Related questions

Cumulative Distinct Customers Over TimeHard · FreeSalary Rank Within DepartmentHard · FreeRunning Total RevenueHard · FreeYear-over-Year GrowthHard · Free7-Day Rolling Revenue AverageHard · FreeMulti-CTE Revenue PipelineHard · Free

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