GROUP BY
GROUP BY is another SQL keyword that we can use to group a set of records in our query by some given criteria. It gathers any data that is identical in the field we want to GROUP BY.
This criteria can change based on what field we want to GROUP BY.
In the following example, we will group pupils by their class and order them by their class in ascending order.
SELECT pupilID, firstname, surname, class, pastoralCareTeacher, meritPointsFROM PupilGROUP BY classORDER BY class ASC;Note: whenever a single query has both GROUP BY and ORDER BY clauses, the GROUP BY clause must precede the ORDER BY clause. If you do not do this, you will get an error.
If we run the query above, a table of results will be returned
Our table would look this this:
| PupilID | First Name | Surname | Class | Pastoral Care Teacher | Merit Points |
| 0005 | Pawel | McTavish | 1I1 | Mrs Aitken | 19 |
| 0002 | David | Boland | 1I2 | Mrs Aitken | 12 |
| 0003 | Amy | Jones | 1M1 | Mr Gilchrist | 55 |
| 0010 | Thomas | Glacney | 1M1 | Miss Armstrong | 91 |
| 0006 | Antonia | Gallagher | 1M2 | Mr Gilchrist | 21 |
| 0001 | Zainab | Singh | 1T1 | Mr Harley | 74 |
| 0004 | James | McNamee | 1T1 | Mr Harley | 88 |
| 0007 | Amy | McNamee | 1T1 | Mr Harley | 63 |
| 0008 | Aysha | Matazinadze | 1T1 | Mr Harley | 45 |
| 0009 | Beatrice | Panner | 1T2 | Mr Harley | 77 |
| PupilID | 0005 |
|---|---|
| First Name | Pawel |
| Surname | McTavish |
| Class | 1I1 |
| Pastoral Care Teacher | Mrs Aitken |
| Merit Points | 19 |
| PupilID | 0002 |
|---|---|
| First Name | David |
| Surname | Boland |
| Class | 1I2 |
| Pastoral Care Teacher | Mrs Aitken |
| Merit Points | 12 |
| PupilID | 0003 |
|---|---|
| First Name | Amy |
| Surname | Jones |
| Class | 1M1 |
| Pastoral Care Teacher | Mr Gilchrist |
| Merit Points | 55 |
| PupilID | 0010 |
|---|---|
| First Name | Thomas |
| Surname | Glacney |
| Class | 1M1 |
| Pastoral Care Teacher | Miss Armstrong |
| Merit Points | 91 |
| PupilID | 0006 |
|---|---|
| First Name | Antonia |
| Surname | Gallagher |
| Class | 1M2 |
| Pastoral Care Teacher | Mr Gilchrist |
| Merit Points | 21 |
| PupilID | 0001 |
|---|---|
| First Name | Zainab |
| Surname | Singh |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 74 |
| PupilID | 0004 |
|---|---|
| First Name | James |
| Surname | McNamee |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 88 |
| PupilID | 0007 |
|---|---|
| First Name | Amy |
| Surname | McNamee |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 63 |
| PupilID | 0008 |
|---|---|
| First Name | Aysha |
| Surname | Matazinadze |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 45 |
| PupilID | 0009 |
|---|---|
| First Name | Beatrice |
| Surname | Panner |
| Class | 1T2 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 77 |
The table above has all pupils grouped by their class and ordered by their class in ascending order (1I1, 1I2, 1M1, 1M2, 1T1, 1T2).