Learn SQL Lesson

OVER Keeps the Detail Rows

A window function calculates across a set of related rows without collapsing the result into one row per group.

That is the big difference from `GROUP BY`. `GROUP BY` shrinks many rows into fewer summary rows. A window function keeps every original row visible and adds extra information beside it.

For example, this query shows every sale and the grand total repeated next to each row:

~~~sql SELECT id, salesperson, amount, SUM(amount) OVER () AS grand_total FROM sales_2 ~~~

`OVER ()` means “use the whole result set as the window.” This is a great first example because it shows the basic idea clearly: same rows, extra context.

Practice challenge

Show every sale with the full-company total beside it. Return id, salesperson, amount, and grand_total, ordered by id.

Open interactive editor