Monthly sales change
GROUP BY month bucket, then LAG for month-over-month
SQL drill • Windows + dates
Table sales(sale_id, sale_date, amount). For each calendar month, show total revenue, the prior month’s total, and the difference.
PostgreSQL below uses DATE_TRUNC('month', …); in MySQL you’d use DATE_FORMAT or similar to build the same grain — see the cheatsheet.
Pipeline
CTE: aggregate → LAG OVER (ORDER BY month)
CTE: aggregate → LAG OVER (ORDER BY month)
Step 0/6Line —
Month buckets → LAG
Press Step or Play.
Ready
First collapse raw rows to one row per month; only then apply LAG on that ordered series.