SQL QuestSQL Interview Questions › Subqueries & CTEs

Month-over-Month Spend Growth by Category

MediumFreeQuerying BasicsSubqueries & CTEsWindow Functions

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_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

merchants

merchant_idnamecategorycountryrisk_tier
1BigBox MartGroceryTRhigh
2Quick StopElectronicsJPhigh
3Aurora CafeTravelTRhigh

Expected output: Clothing 2026-03 28851.98 NULL; Clothing 2026-04 32852.97 13.87; ...

Hint

The hint for this one spells out most of the query, so it stays in the editor. Reach for 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

Department Roster with GROUP_CONCATMedium · FreeConsistent Director AnalysisMedium · FreeBelow Department AverageMedium · FreeHighest Total Salary Budget DepartmentMedium · FreeFare Imputation AnalysisMedium · FreeUNION ALL Dedup: Cross-Dataset SearchMedium · 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