32/34
Part 6 of 6 · Follow-upsLearn and do
Without a window function, a self-join counts the same 7 days
A self-join spells out what the frame means:
SELECT d.ordered_on, COUNT(*) AS orders_7d
FROM (SELECT DISTINCT ordered_on FROM orders) AS d
JOIN orders AS o
ON julianday(o.ordered_on)
BETWEEN julianday(d.ordered_on) - 6 AND julianday(d.ordered_on)
GROUP BY d.ordered_on
ORDER BY d.ordered_on;
| ordered_on | orders_7d |
|---|---|
| 2026-03-01 | 2 |
| 2026-03-02 | 3 |
| 2026-03-03 | 5 |
| 2026-03-09 | 3 |
| 2026-03-10 | 2 |
Same answers as the RANGE frame. The cost is in the join. Each date matches every order in its week, so the work grows with the number of dates times the orders per week. The window sorts once and moves its frame along.
It's a good second route for checking a window query you aren't sure of.
In the editor, write SELF_JOIN_7D: the same 7-day counts, with a self-join.