Saltar al contenido

Grouping with GROUP BY

English
Grouping rows with GROUP BY

An aggregate like AVG normally summarises the whole table into one number. GROUP BY splits the rows into groups that share a value, and gives one summary row per group:

SELECT form, MAX(score) AS best, MIN(score) AS lowest
FROM student
GROUP BY form;
form | best | lowest
11A  | 95   | 88
11B  | 72   | 64

The five rows split into two groups: 11A (Mei 88, Sara 95, Lin 88) and 11B (Jamal 72, Tom 64). Each group becomes one result row, and MAX and MIN work inside each group. COUNT(*), SUM and AVG work the same way.

GROUP BY collapses rows that share a value into one row per group, summing the rest

Material

Iniciar sesión o crear cuenta

IGCSE, A-Level & AP