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

GROUP BY by Column

Function

Groups a table by columns.

Syntax

SELECT attr_expr_list FROM table_reference
 GROUP BY col_name_list;

Keywords

GROUP BY: Can be divided into single-column GROUP BY and multi-column GROUP BY based on the columns.

  • Single-column GROUP BY: The GROUP BY clause contains only one column. The fields in col_name_list must appear in the fields of attr_expr_list. Multiple aggregate functions, such as count(), sum(), can be used in attr_expr_list, and the aggregate functions can contain other fields.
  • Multi-column GROUP BY: The GROUP BY clause contains more than one column. The query statement will group by all fields in the GROUP BY clause. Records with the same values for all fields will be placed in the same group. Similarly, the fields appearing in GROUP BY must be within the fields of attr_expr_list, and aggregate functions can also be used in attr_expr_list.

Precautions

The table to be grouped must already exist. Otherwise, an error is reported.

Example

Group the student table by the score and name fields, and return the grouped results.

SELECT score, count(name) FROM student
   GROUP BY score,name;