Waiting for SQL:202y: GROUP BY ALL

Making GROUP BY a little easier to use is one of the top three requested features in SQL in my experience.

Like, if you do

CREATE TABLE t1 (a int, b int, ...);

SELECT a, avg(b) FROM t1 GROUP BY a;

column list in GROUP BY The section does not give further information. Of course you wanted to form a group aThere is no other suitable option. you can’t create groups a Because that would be an error, and you can’t group by anything other than aBecause there is nothing else in the selection list except the set.

The problem becomes worse if you have long select lists or complex expressions, because you have to repeat these in the GROUP BY clause and keep them carefully updated if you change something. (Or you can try to work around it by using subqueries.) This could be handy!

multiple implementations syntax is adopted GROUP BY ALL To make it simple.

SELECT a, avg(b) FROM t1 GROUP BY ALL;

The SQL Standards Working Group discussed this feature informally at their June 2025 meeting, and it was agreed to move forward.

At the September 2025 meeting, a formal change proposal was put forward and accepted. (So, technically, this is only a working draft right now, and won’t be final until the standard is released.)

formal meaning of GROUP BY ALL That is, it extends to the list of elements of the selection list that do not contain the composite function. so in

SELECT a, avg(b) FROM t1 GROUP BY ALL;

a It does not involve composite function, but avg(b) does, therefore
GROUP BY ALL resolves to GROUP BY aas expected.

This does not completely remove the need for explicit GROUP BY Lists. Consider a more complex case like

CREATE TABLE t1 (a int, b int, c int, d int, ...);

SELECT a, avg(b) + c + d FROM t1 GROUP BY ALL;

Here, a It does not involve composite function, but avg(b) + c + d
It involves an aggregate function, so the query gets resolved

SELECT a, avg(b) + c + d FROM t1 GROUP BY a;

But this is not valid, because you have to account for unsorted columns. c And d somehow. It is not clear what GROUP BY ALL
Should do with this. you could have meant GROUP BY a, c + d or perhaps GROUP BY a, c, d or maybe even GROUP BY a + c + d, GROUP
BY ALL
As currently specified it intentionally does not handle this and leaves it up to the query writer to explicitly write what they want.

Here’s another example:

INSERT INTO t1 VALUES (1, 2, 100), (2, 1, 200), (2, 2, 300);

SELECT a + b, avg(c) FROM t1 GROUP BY ALL;

This means that

SELECT a + b, avg(c) FROM t1 GROUP BY a + b;

No

SELECT a + b, avg(c) FROM t1 GROUP BY a, b;

Which gives a different result.

I’ve found that the documentation for some products with existing implementations is quite hand-written and superficial about details like this, but the standardization should be precise.

The SQL standard doesn’t really support expressions GROUP
BY
List, column reference only, so this last query is not technically conformant. But there is an intention to fix it soon. But then you also need to consider more special cases like user-defined functions or subqueries, so some detail work will be required.

All that said, this type of shortcut syntax does have risks. Like other shortcuts SELECT *If changes are made elsewhere in the command this may cause unintentional changes to the queries. For example, if you have

SELECT a, avg(b) FROM t1 GROUP BY ALL;

and then change it

SELECT a, c, avg(b) FROM t1 GROUP BY ALL;

The reason for this would be GROUP BY Changing the clause implicitly. Maybe that’s not what you meant. This example query is very short, but many times these aggregation queries are long and nested, and someone editing it may not be fully aware that aggregation applies to the query.

So it would probably be a good idea to use this syntax with some caution, and probably use it primarily for interactively written queries and structurally simple queries.

Meanwhile, Oracle Database has also implemented and released GROUP BY ALL Help. (And he did all the work writing the SQL standard change proposal.)

PostgreSQL implemented This happened while I was waiting at the airport to go home from a work group meeting. A patch was already pending, but the hackers group was hesitant to move forward without standardization. So once that happened, the patch was immediately accepted. It will be released next year.




Leave a Comment