Window Seat
You'll answer the questions GROUP BY can't: running totals, ranks within groups, the gap since the last row, streaks, and a calendar with no missing days, all in SQL.
About
A GROUP BY collapses rows. A window function looks across rows and leaves them where they are, which is how you get a running balance, the top three per category, or the days since the previous entry. Then there's the recursive CTE, which lets a database generate its own calendar or walk a tree. Each one takes an evening of being confused before it clicks, and after that most reports you used to build in a spreadsheet are one query. SQLite has had window functions since 3.25 and recursive CTEs for years, so there's no new software to install. What counts is each technique run on your own data, with a result you checked by hand on a few rows.
At a glance
- Per requirement
- 35 XP
- Completion bonus
- 300 XP
- Requirements
- 6
- Takes
- Two or three weeks of evenings
- Skill level
- Intermediate (months of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Write a running total with SUM() OVER (ORDER BY ...) and check three rows by hand. The balance after each transaction, or cumulative distance over a season. Add PARTITION BY to restart it per account or per year. Take a screenshot of the query and the result with the three hand-checked rows marked. Photo
-
2
Rank rows within groups and pick the top three per group. ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) in a CTE, then WHERE rn <= 3 outside it. Try RANK() and DENSE_RANK() on the same data and note where the three differ, which is on ties. Take a screenshot of the query and the result. Photo
-
3
Use LAG to compute the gap between each row and the one before it. In SQLite, julianday(date) - julianday(LAG(date) OVER (ORDER BY date)) gives the days since the previous row; in Postgres, date - LAG(date) OVER (ORDER BY date) does it directly. Find your longest gap and what was going on then. Take a screenshot of the query and the result sorted by gap. Photo
-
4
Find your longest streak of consecutive days with the gaps-and-islands trick. Number the distinct days with ROW_NUMBER, subtract that number from the date (as a day count), and group by the difference; every day in the same streak shares a value. This one takes an evening on its own, and the first answer is usually off by one. Take a screenshot of the query and the streak table with the longest one at the top. Photo
-
5
Generate every date in a year with a recursive CTE and left join your data to it to find the empty days. WITH RECURSIVE days(d) AS (SELECT '2025-01-01' UNION ALL SELECT date(d, '+1 day') FROM days WHERE d < '2025-12-31') is the whole calendar. LEFT JOIN your table to it and count the days with nothing in them, which a GROUP BY on your table alone could never show you. Take a screenshot of the join with the zero days visible. Photo
-
6
Rebuild a report you once made in a spreadsheet using only these techniques, and compare the two. A monthly running total, a per-category leaderboard, a streak count. Take a screenshot of the SQL result beside the spreadsheet with the numbers matching, or with a line on what differed and which one was wrong. Photo