SQL Quest › SQL Interview Questions › Subqueries & CTEs
Month-over-Month Spend Growth by Category
Period-over-period change, the other window shape screens keep. Total card spend per merchant category per calendar month in a CTE, then compare each month to the category's previous month with LAG(spend) OVER (PARTITION BY category ORDER BY month). The first month of every category has no previous row, so its growth is NULL — leave it NULL, do not COALESCE it to 0.
Return category, month (YYYY-MM), spend (rounded to 2 decimals), and mom_growth_pct (100.0 × (spend − previous spend) ÷ previous spend, rounded to 2 decimals). Order by category ascending, then month ascending. The ledger ends on 2026-05-03, so May is a partial month — the drop is real, and the point of reading a growth table is noticing that.
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 |
merchants
| merchant_id | name | category | country | risk_tier |
|---|---|---|---|---|
| 1 | BigBox Mart | Grocery | TR | high |
| 2 | Quick Stop | Electronics | JP | high |
| 3 | Aurora Cafe | Travel | TR | high |
Expected output: Clothing 2026-03 28851.98 NULL; Clothing 2026-04 32852.97 13.87; ...
Hint
SELECT, CTE, Window Functions, and open the hint there if you stall.Concepts
SELECT CTE Window Functions LAG strftime Window Function / LAG
Practise the topic: SQL practice questions · CTE practice · Window function practice
In these company practice sets
Capital One · Ramp · Wise · Bloomberg
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
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