Aggregate functions summarize data over groups of rows from a query result set. The groups are specified using the
GROUP BY clause. They are allowed only in the select list and in the
HAVING and
ORDER BY clauses of a SELECT statement (as described in Aggregate Expressions).
Sorts a query result set on one or more columns.
Syntax
ORDER BY expression [ ASC | DESC ] [, ...]
Semantics
expression
|
can be:
- the name or ordinal number of a SELECT list item
- an arbitrary expression formed from columns that do not appear in the SELECT list
- a CASE expression
|
Notes
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;