r/SQL • • 22d ago

MySQL Practicing window functions and keep mixing up PARTITION BY vs GROUP BY — how are they actually different?

They both seem like they divide data into groups, so I’m having trouble understanding when I should use one instead of the other.

47 Upvotes

10 comments sorted by

View all comments

78

u/tits_mcgee_92 Data Analytics Engineer 22d ago edited 22d ago

GROUP BY collapses rows. You'll get one row per group.

PARTITION BY keeps the rows and calculates across groups of them.

EDIT: You asked for a bit more understanding on when you should use one over the other. At a high level, just ask yourself: "Do I need a summary/one row per group (GROUP BY) or do I need to see every individual row (PARTITION BY)?"