r/SQL • • Oct 20 '26

MySQL COUNT() vs COUNT() OVER()

Post image

I've found this query as a solution to the famous Pizza Runner case study, and I am stuck at figuring out what COUNT() returns, compared to COUNT() OVER().

I know OVER() does the count (or whatever aggregation) over the entire dataset, but idk I'm just not getting the percentage part of this query.

Help :)

29 Upvotes

17 comments sorted by

View all comments

2

u/PrivateFrank Oct 21 '26

It's useful to remember any expressions in the SELECT which has a window clause in it are executed after the GROUP BY (and HAVING) clauses.

So OVER () could be seen as a shortcut for 'ignore grouping'. If you wrote OVER(partition by 1) then the sum of counts would be per row and they would all the percentage values would read 100%.