SteelReed

Search

Search posts, questions…

SQL & Query TuningQuestion 14 / 37

Junior

How do window functions differ from GROUP BY, and ROW_NUMBER from RANK and DENSE_RANK?

Short answer

GROUP BY collapses many rows into one per group. A window function computes over a set of related rows but keeps every row, so you can show details next to totals, ranks and running sums.

GROUP BY customerOVER PARTITION BYcustomerOrder rows1 row per customerdetails lostEvery row kept+ total, rank, running sum

3 points to say:

  1. GROUP BY collapses rows, windows keep them.
  2. RANK 1,1,3; DENSE_RANK 1,1,2.
  3. Running totals: write ROWS explicitly.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.