Home > Articles > Databases

SELECT Statement: The GROUP BY Clause in SQL

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Rick F. van der Lans explains how SQL's GROUP BY clause groups rows on the basis of similarities between them. He goes on to explain the use of adding aggregation functions to display summations, averages, frequencies, and subtotals.

10.1 Introduction

The GROUP BY clause groups rows on the basis of similarities between them. You could, for example, group all the rows in the PLAYERS table on the basis of the place of residence; the result would be one group of players per town. From there you could query how many players there are in each group. The question that is actually answered is then: How many players live in each town? Other examples follow: How many matches have been played per team, and how much has been incurred in penalties per player? In short, the GROUP BY clause is frequently used to formulate questions based on the word per.

By adding aggregation functions, such as COUNT and SUM, to a select block with the use of a GROUP BY clause, data can be aggregated. These functions owe their name to this. Aggregation means that you ask not for the individual values, but for summations, averages, frequencies, and subtotals.

<group by clause> ::=
   GROUP BY <group by specification list>
            [ WITH { ROLLUP | CUBE } ]

<group by specification list> ::=
   <group by specification> [ { , <group by specification> }... ]

<group by specification> ::=
   <group by expression>         |
   <grouping sets specification> |
   <rollup specification>

<grouping sets specification> ::=
   GROUPING SETS ( <grouping sets specification list> )
<grouping sets specification list> ::=
   <grouping sets specification>
   [ { , <grouping sets specification> }... ]

<grouping sets specification> ::=
   <group by expression>                 |
   <rollup specification>                |
   ( <grouping sets specification list> )

<rollup specification> ::=
   ROLLUP ( <group by expression list> ) |
   CUBE ( <group by expression list> )   |
   ( )

<group by expression> ::= <scalar expression>
  • Share ThisShare This
  • Save To Your Account

Discussions

comments powered by Disqus

Related Resources

There are currently no related podcasts. Please check back later.

Careful with those NULLs
By on October 13, 2010Comments

Many folks (including me) use NULL values in their databases. There's actually a bit of controversy on even having them - but I don't fall on the side of never using the. But you do need to exercise some care...

The true value of conferences
By on October 5, 2010Comments

I recently returned from the "SQLBits" conference in York, England. I met a lot of folks that I've seen before at other conferences, but I also met a fair amount that had not been to a SQL Server conference before.

Finding Big Data
By on September 16, 2010Comments

I get asked from time to time about locating "Big Data" - or large sets of data for an application.

See All Related Blogs