GROUP BY Clause
GROUP BY divides a query result set into groups of rows that match an expression.
Syntax
GROUP BY expression [ ,... ]
Semantics
expression
|
is any expression including constants and references to columns in the tables specified in the FROM clause.
Syntax
[ tablename. ] columnname
Semantics
tablename
|
is one of:
- the name of a table
- an alias for a table defined by means of a FROM clause in a query
|
columnname
|
is the name of a column that must be unique across all the tables being used in a query
|
Notes
- There are no space characters in a column reference.
|
Notes
- The expression cannot include aggregate functions.
- All non-aggregated columns in the SELECT list must be included in the GROUP BY clause.
Examples
SELECT C1, C2
FROM T1
GROUP BY C1, C2;
SELECT C1, AVG(C2)
FROM T1
GROUP BY C1;
SELECT x+y, SUM(z)
FROM foo
GROUP BY x+y;
SELECT x+y, y+z, COUNT(*)
FROM foo
GROUP BY x+y, y+z;
SELECT RTRIM(a) || LTRIM(b), AVG(c), COUNT(c)
FROM foo
GROUP BY RTRIM(a) || LTRIM(b);
Invalid Examples
SELECT C1, C2
FROM T1
GROUP BY C1;