As I was practicing SQL on pgexercises.com, I've come across a solution that didn't make sense on a specific line. You can see the question in the link below:
https://pgexercises.com/questions/aggregates/rankmembers.html
The solution query is as shown below, it works but I couldn't really grasp how it doesn't raise an error due to erroneous GROUP BY usage:
select firstname, surname, hours, rank() over (order by hours desc) from
(select firstname, surname,
((sum(bks.slots)+10)/20)*10 as hours
from cd.bookings bks
inner join cd.members mems
on bks.memid = mems.memid
group by mems.memid
) as subq
order by rank, surname, firstname;
What doesn't click on my behalf is: why doesn't including firstname and surname in GROUP BY cause an error? From what I know, the GROUP BY must include all non-aggregate columns that we display to work. Is this because we JOIN by the primary key which identifies each column uniquely, thus somehow negate the necessity to include the display columns in the GROUP BY statement?