A window function calculates a value across related rows without collapsing them into one row, as GROUP BY does. That makes them useful for rankings, running totals and comparing a row with the one before it.
Three common ones
- ROW_NUMBER(): numbers rows within each group, for example each customer’s orders by date.
- RANK(): ranks rows, with ties sharing a number and the next rank skipped.
- LAG(): returns a value from the previous row, useful for month-on-month change.
The pattern
Each one uses OVER (PARTITION BY … ORDER BY …). The partition sets the group, and the order sets the sequence inside it. Run the query on a small table first and check the first few rows by hand.
Start with SQL basics for data analysts, then practise joins in the data analytics course. Book a free demo class, and ask a counsellor for batch details.
