Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it really necessary to have GROUP BY in the SQL standard

Tags:

sql

ansi-sql

After writing SQL for a few years, I find it often annoying having to put the columns I am interested in SELECT and then specify them again in GROUP BY. I can't help thinking, why do we have to do that?

What is the reason that user has to be specific about which column to group by? Can't we just let the SQL engine assume if there is an aggregate function in SELECT, group by the rest non-aggregate columns?

This will be especially helpful and more concise when you have a large CASE WHEN in SELECT.

like image 868
Peisong Cong Avatar asked Nov 19 '25 07:11

Peisong Cong


1 Answers

Because they may not always match exactly.

For example, If I want find out the maximum number of books per category, I could do:

select max(cnt)
from (
    select count(*) as cnt
    from books
    group by category
    ) t;

In some DBs such as Oracle, you can even do this:

select max(count(*))
from books
group by category;

I don't really need to specify the category column as I don't need it.

A few databases such as Postgres support the use of aliases in the group by clause.

like image 99
Gurwinder Singh Avatar answered Nov 20 '25 21:11

Gurwinder Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!