Back to Questdb

GROUP BY keyword

documentation/query/sql/group-by.md

latest2.0 KB
Original Source

Groups aggregation calculations by one or several keys. In QuestDB, this clause is optional.

Syntax

:::note

QuestDB groups aggregation results implicitly and does not require the GROUP BY keyword. It is only supported for convenience. Using the GROUP BY clause explicitly will return the same results as if the clause was omitted.

:::

Examples

The below queries perform aggregations on a single key. Using GROUP BY explicitly or implicitly yields the same results:

questdb-sql
SELECT symbol, avg(price)
FROM trades
GROUP BY symbol;
questdb-sql
SELECT symbol, avg(price)
FROM trades;

The below queries perform aggregations on multiple keys. Using GROUP BY explicitly or implicitly yields the same results:

questdb-sql
SELECT symbol, side, avg(price)
FROM trades
GROUP BY symbol, side;
questdb-sql
SELECT symbol, side, avg(price)
FROM trades;

When used explicitly, the list of keys in the GROUP BY clause must match the list of keys in the SELECT clause, otherwise an error will be returned:

questdb-sql
SELECT a, b, avg(temp)
FROM tab
GROUP BY a;
questdb-sql
SELECT a, avg(temp)
FROM tab
GROUP BY a, b;
questdb-sql
SELECT a, b, avg(temp)
FROM tab
GROUP BY a, b;

See also