Home / Learn Secure SQL / Window Functions

Window Functions

Calculations across rows without collapsing them.

The gistCalculations across rows without collapsing them.

The idea, explained simply

Window vs GROUP BYGROUP BY: feweroutput rowswindow function: samerow countextra computed columnadded
Window vs GROUP BY

What it is

A window function computes a value (like a running total or a rank) across a set of related rows, while still returning one row per input row, unlike GROUP BY which collapses rows.

Why it matters

They are common in reports: "rank within category", "running total", "compare to the previous row".

Mechanically speaking

Use an aggregate or ranking function with OVER(...), optionally with PARTITION BY to reset per group.

Shows up in

  • Web application code review
  • Backend development
  • Database administration
Ranking orders per userSQL: edit and run

Each user's own orders are ranked 1, 2, ... by size, independently of other users, and every row is still returned.

Check yourself

1. Unlike GROUP BY, a window function...