Updated on 2026-11-06 GMT+08:00

GROUP BY Expression

Function

Groups a table by an expression.

Syntax

SELECT attr_expr_list FROM table_reference
 GROUP BY groupby_expression [, groupby_expression, ...];

Keywords

groupby_expression: Can be a single field, multiple fields, or an aggregate function, string function, etc.

Precautions

  • The table to be grouped must already exist. Otherwise, an error is reported.
  • Similar to single-column grouping, the fields appearing in GROUP BY must be included in the fields of attr_expr_list. Expressions support built-in functions, user-defined functions, etc.

Example

Use the substr function to extract a substring of the name field, group by that substring, and return each substring and its corresponding record count.

SELECT substr(name,6),count(name) FROM student
   GROUP BY substr(name,6);