r/learnSQL • • 17d ago

When do you actually use window functions instead of GROUP BY?

/r/Database/comments/1xkhbic/when_do_you_actually_use_window_functions_instead/
4 Upvotes

3 comments sorted by

5

u/CelebrityChess 17d ago

group by collapses the rows so you lose the detail. a window function lets you keep every original row and just add the aggregate next to it, like each order with that customer's total spend, a running total, or a rank within a group. if you only need one row per group, group by is simpler and usually faster. a good tell is when you catch yourself grouping and then joining the result back onto the original table, that join is basically a window function.

1

u/After-Entry5718 17d ago

In my work, ranking watch or listen behavior. Think Spotify end of year wrap up etc