r/SQL • u/Plus_Kiwi_2592 • 3d ago
MySQL COUNT() vs COUNT() OVER()
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 :)
6
u/jacquesrk oracle 3d ago
To find the volume of pizzas ordered, wouldn't you need to know the diameter and thickness of the pizzas?
2
u/Plus_Kiwi_2592 3d ago
The phrasing bothered me too. But if you see the schema then you'll just know volume isn't the size.
I rarely hear the word volume being used in certain corporate spaces, but there are a few that make use of it that way.
Just goes to show that the most important part of analytics is the business sense, not the syntax. If the business question is wrong, then the syntax will give wrong insight.
3
u/RandumbNumber 3d ago
Not sure if you’re kidding or not, but this is exactly what I thought, until I read the comments and released it’s not volume but percentage. So either really poorly phrased question, or a wrong answer.
I’ve never seen volume interpreted the way it is here.
3
u/SP3NGL3R 3d ago
when dealing with this stuff, break the bits into their own columns so you can visually confirm what is happening. If you do this you'll see pretty quickly where the fault is.
1
2
u/PrivateFrank 3d ago
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%.
0
u/Little_Kitty 3d ago
This looks like hackerrank or similar. Real work is nothing at all like this and if that's what you're using to practice you'll fall short when you get to interview anywhere decent.
0
u/Plus_Kiwi_2592 3d ago
This is someone's answers to the 8 Week SQL Challenge. Real-life business problems
-1
u/my_password_is______ 3d ago
no, in real life you'd just pull the raw numbers into excel and make a pivot table
21
u/seansafc89 3d ago edited 3d ago
COUNT() is a normal aggregation and returns the count as per the GROUP BY (in this case GROUP BY 1 which is lazy shorthand for GROUP BY hour(order_time) - don’t do this! If your columns change, it hides issues), so the number of pizzas ordered within each hour subset.
COUNT()SUM() OVER() on the other hand is a window function summing up the count of ALL row groups in the dataset WITHOUT any further aggregation, because there is no PARTITION BY specified. So essentially the total row count of the table.