Day 309 — The SQL you keep rewriting in pandas, written once
Friday, 11 June 2027 · Week 45 of 52 · Production · Study day · 60 min
Half the transformations in your notebooks are window functions, and writing them where the data lives is faster, shorter and the version an interviewer will ask you to produce.
What you should be able to do tonight
- Write a rolling aggregate per symbol as a window function and explain the frame clause.
- Say when computing in SQL beats pulling into pandas, in terms of what crosses the process boundary.
Tasks 60 min
35 min Work through the window functions material on Modern SQL, then write four queries against your own DuckDB store: a 20-day rolling mean per symbol, a rank within each date, a lagged value per symbol, and a cumulative sum reset at each year. Get the partition and frame clauses right rather than approximately right.
25 min Measure the boundary: time each query computed in SQL against the equivalent pulled into pandas, for a full universe over ten years. Report both times and the peak memory. The gap is mostly serialisation, and knowing that is what tells you which side of the boundary a computation belongs on.
Resources
- Modern SQL (Winand) — window functions, CTEs reference
- DuckDB documentation tool
Deliverable
Four window-function queries against your store, each timed against its pandas equivalent with peak memory reported.
Checkpoint
Write a query that computes a 20-day rolling mean per symbol from a trades table, then say why doing it in SQL might be the right call.
Prepare for tomorrow
Ten minutes tonight so Day 310, Saturday, 12 June 2027 starts clean. This is what the evening email sends you.
Work through the first eight lessons of SQLBolt if joins are rusty, or the aggregation section of PostgreSQL Exercises if they are not.
Twenty minutes maximum.
Note which construct you had to look up.