GROUP BY vs a window function?
Understand the concept
GROUP BY collapses source rows into one row per group, while window functions calculate across related rows without removing the individual rows. A window is defined by partitions, ordering and sometimes a frame. This makes windows useful when a report needs both detail and comparisons such as department rank.
Important ideas
-
GROUP BY turns each group into a summary row.
-
A window function keeps the original rows and adds a calculation.
-
Use
PARTITION BYfor separate groups andORDER BYfor calculation order. -
The ORDER BY inside OVER controls the window calculation, not necessarily the final display order.
Can you explain it without the notes?
GROUP BY vs a window function?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
GROUP BY collapses source rows into one row per group, while window functions calculate across related rows without removing the individual rows. A window is defined by partitions, ordering and sometimes a frame. This makes windows useful when a report needs both detail and comparisons such as department rank.
Turn this concept into a clear answer.
- Say whether the result must preserve each original row; that often decides between grouping and a window.
Adapted from 05_SQL_POSTGRESQL.md · “GROUP BY vs a window function?”. Examples are study fragments; framework behavior can depend on your version.