Do all columns in a SELECT list have to appear in a GROUP BY clause

后端 未结 7 1230
悲&欢浪女
悲&欢浪女 2020-11-30 19:50

My lecturer stated:

All column names in SELECT list must appear in GROUP BY clause unless name is used only in an aggregate function

I\'m ju

7条回答
  •  广开言路
    2020-11-30 20:17

    Imagine the following:

        A    B    C
       Cat   10   False
       Dog   25   True
       Dog   20   False
       Cat   5    False
    

    If you select A, B and Group By Only A - what would your output be? You'd only have two rows (or tuples) because you have two values for A - but how does it display B?

    If you group by A, B, you'd get four rows, no problems there. If you group by A and perform a function on B - like SUM(B) then you get two rows again:

        Cat    15
        Dog    45
    

    But if you select A, B and only group by A - it doesn't know what to do. Truthfully, I believe there are some databases out there that will select a random value for B in that case and I believe there are some that will give you an error message.

提交回复
热议问题