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

Syntax

Flow chart showing the syntax of the GROUP BY keyword

:::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:

  1. SELECT sensorId, avg(temp)
  2. FROM readings
  3. GROUP BY sensorId;
  1. SELECT sensorId, avg(temp)
  2. FROM readings;

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

  1. SELECT sensorId, sensorType, avg(temp)
  2. FROM readings
  3. GROUP BY sensorId,sensorType;
  1. SELECT sensorId, sensorType, avg(temp)
  2. FROM readings;

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:

  1. SELECT a, b, avg(temp)
  2. FROM tab
  3. GROUP BY a;
  1. SELECT a, avg(temp)
  2. FROM tab
  3. GROUP BY a, b;
  1. SELECT a, b, avg(temp)
  2. FROM tab
  3. GROUP BY a, b;