Is SQL GROUP BY a design flaw? [closed]
Asked Answered
B

9

33

Why does SQL require that I specify on which attributes to group? Why can't it just use all non-aggregates?

If an attribute is not aggregated and is not in the GROUP BY clause then nondeterministic choice would be the only option assuming tuples are unordered (mysql kind of does this) and that is a huge gotcha. As far as I know, Postgresql requires that all attributes not appearing in the GROUP BY must be aggregated, which reinforces that it is superfluous.

  • Am I missing something or is this a language design flaw that promotes loose implementations and makes queries harder to write?
  • If I am missing something, what is an example query where group attributes can not be inferred?   
Broiler answered 22/2, 2010 at 13:14 Comment(4)
We'll forward your concerns to the ANSI SQL Committee.Rosati
Hehe. That's the thing. It's designed by some very smart people so there must be something I'm not seeing.Broiler
I've said so for a long time.Vanquish
It seems some many thousands of people already did forward such concerns to the ANSI committee, so they changed the definition of "group by" in ANSI 1999 SQL. This is described in ypercube's answer. Plenty of things designed by "really smart people", and especially committees of same, are hopelessly broken^W^W very far from ideal.Bankston
H
14

You don't have to group by the exactly the same thing you're selecting, e.g. :

SQL:select priority,count(*) from rule_class
group by priority

PRIORITY COUNT(*) 70 1 50 4 30 1 90 2 10 4

SQL:select decode(priority,50,'Norm','Odd'),count(*) from rule_class group by priority

DECO COUNT(*) Odd 1 Norm 4 Odd 1 Odd 2 Odd 4

SQL:select decode(priority,50,'Norm','Odd'),count(*) from rule_class group by decode(priority,50,'Norm','Odd')

DECO COUNT(*) Norm 4 Odd 8

Hephaestus answered 22/2, 2010 at 13:50 Comment(2)
Of course. All non-aggregate attributes in the SELECT must appear in the GROUP BY but not all attributes in the GROUP BY must appear in the SELECT. I feel dumb for not checking the converse. But there is still a case for making GROUP BY optional in the presence of a mix of aggregate and non-aggregate SELECT columns, a common use case. I really hate the mysql behavior of just randomly choosing a column when the SELECT attribute isn't accounted for in the GROUP BYBroiler
Er, I mean when mysql just chooses an arbitrary tuple.Broiler
T
6

There is one more reason for why does SQL requires that I specify on which attributes to group.

Lets sat we have two simple tables: friend and car, where we store info about our friends and their cars.

And lets say we want to show all our friends's data (from table friend) and for everyone of our friends, how many cars they own now, have sold, have crashed and the total number. Oh, and we want the elders first, younger last.

We'd do something like:

SELECT f.id
     , f.firstname
     , f.lastname
     , f.birthdate
     , COUNT(NOT c.sold AND NOT c.crashed) AS owned
     , COUNT(c.sold) AS sold
     , COUNT(c.crashed) AS crashed
     , COUNT(c.friendid) AS totalcars
FROM friend f
LEFT JOIN car c     <--to catch (shame!) those friends who have never had a car 
  ON f.id = c.friendid
GROUP BY f.id
       , f.firstname
       , f.lastname
       , f.birthdate
ORDER BY f.birthdate DESC

But do we really need all those fields in the GROUP BY? Isn't every friend uniquely determined by his id? In other words, aren't the firstname, lastname and birthdate functionally dependend on the f.id? Why not just do (as we can in MySQL):

SELECT f.id
     , f.firstname
     , f.lastname
     , f.birthdate
     , COUNT(NOT c.sold AND NOT c.crashed) AS owned
     , COUNT(c.sold) AS sold
     , COUNT(c.crashed) AS crashed
     , COUNT(c.friendid) AS totalcars
FROM friend f
LEFT JOIN car c     <--to catch (shame!) those friends who have never had a car 
  ON f.id = c.friendid
GROUP BY f.id
ORDER BY f.birthdate 

And what if we had 20 fields in the SELECT (plus ORDER BY) parts? Isn't the second query shorter, clearer and probably faster (in the RDBMS that accept it)?

I say yes. So, do the SQL 1999 and 2003 specs say, if this article is correct: Debunking group by myths

Tamartamara answered 9/4, 2011 at 22:25 Comment(6)
The most recent SQL standards say, in effect, that you can include columns in the SELECT list that aren't in the GROUP BY list if and only if those columns are functionally dependent on columns that are in the GROUP BY list. The objections to MySQL usually have to do with its allowing you to include columns in the SELECT list that aren't functionally dependent on any columns in the GROUP BY list.Quadrature
@Catcall: I agree on that. MySQL's deviation from this and the "random" rows it fetches when one uses this "feature", opens the door to problems.Folkways
@MikeSherrill'CatRecall' I was looking for such standard. Would you have a link? I failed to see anything in ANSI 1992 at least that requires all columns to be grouped by anyway.Vieva
@Vieva drafts of the latest standard (2003+) are available at this site: wiscorp.com The 20nn.zip has 8 or 9 pdfs. The main pdf about SQL is the -02-.Folkways
There is another site that I found to have: SQL1992 The part about group and columns in select list is in page 192: "7) If T is a grouped table, then each <column reference> in each <value expression> that references a column of T shall reference a grouping column or be specified within a <set function specification>."Folkways
@ypercube I shall be eternally grateful to thee, kind sir!Vieva
M
3

I would say if you have a large number of items in the group by clause then perhaps the core info should be pulled out into a tabular sub-query which you inner join into.

There is a probably a performance hit, but it makes for neater code.

select  id, count(a), b, c, d
from    table
group by
        id, b, c, d

becomes

select  id, myCount, b, c, d
from    table t
        inner join (
            select id, count(*) as myCount
            from table
            group by id
        ) as myCountTable on myCountTable.id = t.id

That said, I'm interested to hear counter-arguments for doing this as opposed to having a large group by clause.

Maricamarice answered 22/2, 2010 at 14:44 Comment(2)
it makes for performance improvements too, as it typically means less sort/temp/work space being used.Dominy
Yeah I agree that this is the way to go. Not keen on the extra complexity of (additional) nested queries, but what can we do.Bankston
W
3

I agree its verbose that the group by list shouldn't implicitly be the same as then non-aggregated select columns. In Sas there are data aggregation operations that are more succinct.

Also : it's hard to come up with an example where it would be useful to have a longer list of columns in the group list than the select list. The best I can come up with is ...

create table people
(  Nam char(10)
  ,Adr char(10)
)

insert into people values ('Peter', 'Tibet')
insert into people values ('Peter', 'OZ')
insert into people values ('Peter', 'OZ')

insert into people values ('Joe', 'NY')
insert into people values ('Joe', 'Texas')
insert into people values ('Joe', 'France')

-- Give me people where there is a duplicate address record

select * from people where nam in 
(
select nam              
from People        
group by nam, adr        -- group list different from select list
having count(*) > 1
)
Wensleydale answered 23/2, 2010 at 18:9 Comment(0)
U
2

If you issue just regarding to easier way to write scripts. Here is one tip:

In MS SQL MGMS write you query in text something like select * from my_table after that select text right click and "Design Query in Editor.." Sql studio will open new editor with filed up all fields after that again right click and select "Add Gruop BY" Sql MGM studio will add code for you .

I fund this method extremely useful for insert statements. When I need to write script for insert a lot of fields in table, I just do select * from table_where_want_to_insert and after that change type of select statement to insert,

Umber answered 22/2, 2010 at 13:25 Comment(0)
G
2

I Agree

I quite agree with the question. I asked the same one here.

I honestly think it's a language flaw.

I realise that there are arguments against that, but I have yet to use a GROUP BY clause containing anything other than all the non-aggregated fields from the SELECT clause in the real world.

Gloriole answered 22/2, 2010 at 14:37 Comment(0)
C
1

This thread provides some useful explanations.

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/52482614-bfc8-47db-b1b6-deec7363bd1a

Cherin answered 22/2, 2010 at 13:44 Comment(0)
M
1

I'd say it is more likely to be a language design choice that decisions be explicit, not implicit. For instance, what if I wish to group the data in a different order than that in which I output the columns? Or if I want to group by columns that aren't included in the columns selected? Or if I want to output grouped columns only and not use aggregate functions? Only by explicitly stating my preferences in the group by clause are my intentions clear.

You also have to remember that SQL is a very old language (1970). Look at how Linq flipped everything around in order to make Intellisense work - it looks obvious to us now, but SQL predates IDEs and so couldn't have taken into account such issues.

Mitochondrion answered 22/2, 2010 at 13:44 Comment(1)
+1 for mentioning the importance of the ordering of the fields in an ORDER BY. A ... GROUP BY a, b may deliver different results when compared to ... GROUP BY b, aDysplasia
O
0

The "superflous" attributes influence the ordering of the result.

Consider:

create table gb (
  a number,
  b varchar(3),
  c varchar(3)
);

insert into gb values (   3, 'foo', 'foo');
insert into gb values (   1, 'foo', 'foo');
insert into gb values (   0, 'foo', 'foo');

insert into gb values (  20, 'foo', 'bar');
insert into gb values (  11, 'foo', 'bar');
insert into gb values (  13, 'foo', 'bar');

insert into gb values ( 170, 'bar', 'foo');
insert into gb values ( 144, 'bar', 'foo');
insert into gb values ( 130, 'bar', 'foo');

insert into gb values (2002, 'bar', 'bar');
insert into gb values (1111, 'bar', 'bar');
insert into gb values (1331, 'bar', 'bar');

This statement

select sum(a), b, c
  from gb
group by b, c;

results in

    44 foo bar
   444 bar foo
     4 foo foo
  4444 bar bar

while this one

select sum(a), b, c
  from gb
group by c, b;

results in

   444 bar foo
    44 foo bar
     4 foo foo
  4444 bar bar
Outtalk answered 22/2, 2010 at 13:42 Comment(2)
The only thing that determines ordering is an ORDER BY clause. If you don't explicitly state the desired order the data should be returned, the database can order it however is most convenient to it. This will vary from database to database. Without an ORDER BY clause, any appearance of order is entirely coincidental.Mitochondrion
-1 GROUP BY does not imply SORT. Parallelism or hash aggregates could both lead to results not sorted in the way you appear to expect. Additionally in SQL Server (and quite possibly other RDBMSs too) if you GROUP BY PK, Other Functionally Dependant Cols it completely ignores these other columns in the GROUP BY operation they are just required in the GROUP BY to be syntactically valid.Goyette

© 2022 - 2024 — McMap. All rights reserved.