Why does SQL force me to repeat all non-aggregated fields from my SELECT clause in my GROUP BY clause? [closed]

I tend to agree with you – this is one of many cases where SQL should have slightly smarter defaults to save us all some typing. For example, imagine if this were legal:

Select ClientName, InvoiceAmount, Sum(PaymentAmount) Group By *

where “*” meant “all the non-aggregate fields”. If everybody knew that’s how it worked, then there would be no confusion. You could sub in a specific list of fields if you wanted to do something tricky, but the splat means “all of ’em” (which in this context means, all the possible ones).

Granted, “*” means something different here than in the SELECT clause, so maybe a different character would work better:

Select ClientName, InvoiceAmount, Sum(PaymentAmount) Group By !

There are a few other areas like that where SQL just isn’t as eloquent as it could be. But at this point, it’s probably too entrenched to make many big changes like that.

Leave a Comment