views:

89

answers:

5

I'm working on a mock exam paper at the moment, however I have no set of correct answers and I'm not sure what the correct answer of this SQL query is.

Given a table:

foo, bar
a  , 1
b  , 3
a  , 2
c  , 1

and the query:

SELECT foo, sum(bar)
FROM table
GROUP BY foo

The two ways I can see this going are either:

a 3
a 3
b 3
c 1

or

a 3
b 3
c 1

Thanks.

+6  A: 

It will be the last one.

GROUP BY will cause the set to group to the values of foo - a single line per each. The SUM aggregate function will sum the values of the bar columns.

Oded
is SQL clever enough not to collapse different results together then? As in if sum was different for different a's they would not be merged somehow
Martin
@Martin - The sum of all `bar` values associated with `a` will always be the same. The fact that there are other rows between the `a` rows is not important.
Oded
@martin that can't happen since there's only going to be one sum for a. All 'a's will have their bar value summed yielding one result for the group 'a'. Which is not any particular 'a' row
Rune FS
I meant that if the sum(bar) was something else (which may vary, say sum(bar) * bash) then presumably SQL would not collapse them?
Martin
`sum(bar) * bash` would return the sum of `bar`s multiplied by `bash`.
Oded
When grouping (or colapsing) as you Call it only the group by column(s) is considered. The result doesn't matter and in fact can't be calculated before the groups are known
Rune FS
+2  A: 

This one:

SELECT  foo, sum(bar)
FROM    table
GROUP BY
        foo

will give:

a 3
b 3
c 1

This one:

SELECT  foo, SUM(bar) OVER (PARTITION BY foo)
FROM    table

will give:

a 3
a 3
b 3
c 1
Quassnoi
+3  A: 

GROUP BY combines all rows that have the same value names in the GROUP BY. The SUM tell the query what to do with the values not named in the GROUP BY.

foo, bar
a  , 1
b  , 3
a  , 2
c  , 1

SELECT foo, sum(bar)
FROM table
GROUP BY foo

so you'll get 1 row for each foo: a, b and c, and it will SUM the bar values for each of those groups: a(1+2), b(1), c(1)

and result in your second result set:

a 3
b 3
c 1

Since this is homework, you need to learn:

FROM and JOIN determine & filter rows
WHERE more filters on the rows
GROUP BY combines those rows into groups
HAVING filters groups
ORDER BY arranges the remaining rows/groups

KM
+2  A: 

the group by will take effect first so the "computation" wil go like this

first group all rows on the value of foo. giving you

  • group1: a,1 a,2
  • group2: b,3
  • group3: c,1

then produce a result row for each group based on the aggregate function sum. That is summing the value of bar for each group i.e.

  • a,3
  • b,3
  • c,1
Rune FS
+1  A: 

The query:

select foo, sum(bar)
from table
group by foo

will give you the second set of output:

a 3
b 3
c 1

but not necessarily in that order. It may also give you those three lines in any other order. A group by clause does not mandate the order in which rows are returned.

paxdiablo
FWIW, `GROUP BY` does mandate the order in `MySQL`, unless appended with `ORDER BY NULL`.
Quassnoi
I prefer to code to the standard unless absolutely necessary. I don't like to rely on the behaviour of one DBMS then find out it breaks when I want to move - that's how vendors get their lock-in.
paxdiablo