LESSON 08 / 24·2 min readIntermediate

GROUP BY vs a window function?

01 Understand02 Recall03 Prepare

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 BY for separate groups and ORDER BY for calculation order.

  • The ORDER BY inside OVER controls the window calculation, not necessarily the final display order.

PAUSE & RECALL

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Say whether the result must preserve each original row; that often decides between grouping and a window.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: ROW_NUMBER vs RANK vs DENSE_RANK?

Adapted from 05_SQL_POSTGRESQL.md · “GROUP BY vs a window function?”. Examples are study fragments; framework behavior can depend on your version.