ข้ามไปยังเนื้อหา
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 fruit turns apple 2, pear 1, apple 4, pear 3 into apple 6 and pear 4

เอกสารประกอบการเรียน

เข้าสู่ระบบหรือสร้างบัญชี

IGCSE, A-Level & AP