The advanced SQL that turns reports into one query
Window functions compute values across rows related to the current row, without collapsing them into groups. CTEs (Common Table Expressions) let you name and reuse subqueries — and recursive CTEs handle hierarchical data.
Window function shapes you'll use:
row_number() OVER (ORDER BY ...)— sequential number per ordering.rank()/dense_rank()— competition-style ranking.sum(...) OVER (...)/avg(...) OVER (...)— running totals, moving averages.lag(col)/lead(col)— previous/next row's value.partition by— windows reset per partition.
Tip: Recursive CTE is SQLite's general tool for hierarchies: org charts, file trees, comment threads. The shape is always
WITH RECURSIVE name AS (anchor UNION ALL recursive_step) SELECT .... Once you internalize the pattern it replaces a lot of application-side traversal code.