What's the logic behind what is selected when you don't use group by?


#1

This code gives the percetage of total sales for each item, categorised as instructed:

select
case name
when 'kale-smoothie' then 'smoothie'
when 'banana-smoothie' then 'smoothie'
when 'orange-juice' then 'drink'
when 'soda' then 'drink'
when 'blt' then 'sandwich'
when 'grilled-cheese' then 'sandwich'
when 'tikka-masala' then 'dinner'
when 'chicken-parm' then 'dinner'
else 'other'
end as category, round (1.0 * sum(amount_paid) /
(select sum(amount_paid) from order_items) * 100, 2) as pct
from order_items
group by 1
order by 2 desc;

If I take out the "group by 1" line, I get this output:

category | pct
drink | 100.0

I'm not grouping the data, but I am summing it, so I would expect to get one row back like this. By why drink? In the previous exercises (the same thing but where you ask for all product names, NOT the categories), I get orange juice. Which is a drink. So it seems it is defaulting to one particular record as the one it displays.

I thought it might be the first row in the table, but that's not orange juice.

Anyone know why it gives this output?

I'm just trying to play around and see how it all works.

Thanks,
FL