documentation/query/sql/group-by.md
Groups aggregation calculations by one or several keys. In QuestDB, this clause is optional.
:::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.
:::
The below queries perform aggregations on a single key. Using GROUP BY
explicitly or implicitly yields the same results:
SELECT symbol, avg(price)
FROM trades
GROUP BY symbol;
SELECT symbol, avg(price)
FROM trades;
The below queries perform aggregations on multiple keys. Using GROUP BY
explicitly or implicitly yields the same results:
SELECT symbol, side, avg(price)
FROM trades
GROUP BY symbol, side;
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:
SELECT a, b, avg(temp)
FROM tab
GROUP BY a;
SELECT a, avg(temp)
FROM tab
GROUP BY a, b;
SELECT a, b, avg(temp)
FROM tab
GROUP BY a, b;