Theory
OVER() changes the game
A window function computes a value across a set of rows related to the current row, without collapsing the result like GROUP BY does. The three you will use every week:
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY event_ts DESC)— deduplicate to latest event per user.LAG(amount, 1) OVER (PARTITION BY account ORDER BY ts)— compute deltas without a self-join.SUM(amount) OVER (PARTITION BY account ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)— running balance.
Window functions are the dividing line between SQL you can produce in an interview and SQL that runs a real warehouse.