GROUP BY vs a window function?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn GROUP BY vs a window function?
Definition:
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.
Key points:
-
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.
Interview point:
- 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?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.