One answer per group
After this: Count students per city and marks per course with GROUP BY, one result row per group.
On this page · 5 sections · 3 checks
So far every count has been one number for the whole table. Now the head teacher asks: how many students live in each city? You could write one query per city: WHERE city = 'Hull', then Leeds, then York. That works for three cities, but you must know the cities first, and fifty cities would need fifty queries.
Piles of rows
Think of the six students as six cards on a desk. To count them per city, you sort the cards into piles, one pile for each city, and then count each pile.
SQL does the same. Each pile is a : the rows that have the same value in one column. You ask for groups with and the column's name.
SELECT city, COUNT(*) AS studentsFROM studentsGROUP BY cityORDER BY city;Work through it:
- FROM brings in the 6 students.
- GROUP BY city sorts them into 3 groups: Hull holds Dev; Leeds holds Ana, Chloe and Femi; York holds Ben and Eve.
- SELECT gives back one row per group: the city, and
COUNT(*)counted inside that group. Hull 1, Leeds 3, York 2. - ORDER BY sorts the three rows by city.
Press Run it and step through. At the GROUP BY step the rows are drawn as bands, one band per group.
SELECT city, COUNT(*) AS studentsFROM studentsGROUP BY cityORDER BY city;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
The rule: with GROUP BY, each aggregate function runs once for each group, and the query gives back one row per group. Without GROUP BY, as in lesson 1, the whole table is one group, and you get one row.
Marks per course
The same idea answers "how is each course doing?". Group the enrolments by course_id, and count and average inside each group.
SELECT course_id, COUNT(*), COUNT(mark), AVG(mark)FROM enrolmentsGROUP BY course_idORDER BY course_id;enrolments · 10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Read the four rows:
| course_id | COUNT(*) | COUNT(mark) | AVG(mark) |
|---|---|---|---|
| 1 Maths | 4 | 4 | 76.5 |
| 2 Physics | 2 | 2 | 56 |
| 3 History | 2 | 1 | 80 |
| 4 Art | 2 | 2 | 83 |
Course 3 shows lesson 2 again, inside one group. It has two rows, Ana's 80 and Chloe's NULL. COUNT(*) counts both rows, COUNT(mark) counts one mark, and the average is the one mark, 80.
Course 5, Music, is not in the result. Nobody takes it, so no enrolment row has course_id 5, and GROUP BY has no row to make a group from. No rows, no group. The next tutorial shows how to list Music with a count of 0, by reading the courses table and the enrolments table together.
SQLite's own shell prints those averages as 56.0, 80.0 and 83.0, because AVG always gives a decimal number. This page writes them without the .0.
Run it here. Then change course_id to student_id in all three places, and you get one row per student.
Several answers per group
One query can ask several questions of each group. Here, for each student: how many courses, and their best mark.
SELECT student_id, COUNT(*) AS courses, MAX(mark) AS bestFROM enrolmentsGROUP BY student_idORDER BY student_id;| student_id | courses | best |
|---|---|---|
| 1 | 3 | 80 |
| 2 | 2 | 90 |
| 3 | 2 | 85 |
| 4 | 2 | 76 |
| 6 | 1 | 91 |
Eve, student 5, takes no course, so she has no rows and no group.
What you may put in SELECT
Look at what each result row can hold. city is fine: in one group every row has the same city. COUNT(*) is fine: it gives one number per group. But name is not: the Leeds group holds three names, and one row has room for one.
The rule: every column in SELECT is either named in GROUP BY or inside an aggregate function.
Check yourself
The six students are in years 2, 1, 2, 3, 1 and 3. How many rows does this query give back?
SELECT year, COUNT(*)FROM studentsGROUP BY year;GROUP BY course_id runs on all ten enrolments. Which groups does it make? Each chip is one group: its course_id, then the marks in it.
Complete the query, so that it gives one row per student: the student's id and how many courses they take.
SELECT student_id, COUNT(*) AS coursesFROM enrolments____________________ -- ← one row per studentORDER BY student_id;| student_id | courses |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
| 6 | 1 |
- Split the rows into groups with GROUP BY, one group for each different value of a column.
- Say that each aggregate function then runs once per group, and the query gives back one row per group.
- Say why a value that appears in no row, such as a course nobody takes, has no group and no row in the result.
- Keep to the rule: every column in SELECT is either named in GROUP BY or inside an aggregate function.