Keeping some groups
After this: Keep only the groups that pass a test with HAVING, and use WHERE and HAVING in one query.
On this page · 4 sections · 3 checks
Lesson 3 gave one row for each course. Now the head teacher wants a shorter list: only the courses whose average is 75 or more. In the last tutorial you kept some rows and dropped others with WHERE. So the first thing to try is WHERE.
WHERE cannot see groups
SELECT course_id, AVG(mark)FROM enrolmentsWHERE AVG(mark) >= 75GROUP BY course_id;SQLite refuses to run it, with the message misuse of aggregate: AVG(). The reason is the order in which the database works:
- FROM brings in the rows.
- WHERE tests one row at a time and drops the rows that fail.
- GROUP BY makes the groups.
- SELECT works out the averages.
When WHERE runs, there are no groups yet, so there is no average to test. One row has a mark, but it has no average.
HAVING tests groups
The test has to happen after the groups are made. That is what is for. It comes right after GROUP BY, and it can use aggregate functions.
SELECT course_id, AVG(mark)FROM enrolmentsGROUP BY course_idHAVING AVG(mark) >= 75ORDER BY course_id;| course_id | AVG(mark) |
|---|---|
| 1 | 76.5 |
| 3 | 80 |
| 4 | 83 |
Work through it:
- FROM brings in the 10 enrolments.
- GROUP BY course_id makes 4 groups.
- HAVING works out each group's average and tests it: course 1 has 76.5 (kept), course 2 has 56 (dropped), course 3 has 80 (kept), course 4 has 83 (kept).
- SELECT gives one row for each group that is left, and ORDER BY sorts them.
HAVING can test any aggregate. "Courses with more than two students" is a test on the count:
SELECT course_id, COUNT(*)FROM enrolmentsGROUP BY course_idHAVING COUNT(*) > 2;| course_id | COUNT(*) |
|---|---|
| 1 | 4 |
WHERE and HAVING in one query
Now a question that needs both. Which courses have at least two students who passed, with a mark of 60 or more?
- "a mark of 60 or more" is a test of one row. That is WHERE.
- "at least two students" is a test of a group. That is HAVING.
First, the query with WHERE only, to see the counts:
SELECT course_id, COUNT(*) AS passedFROM enrolmentsWHERE mark >= 60GROUP BY course_idORDER BY course_id;| course_id | passed |
|---|---|
| 1 | 3 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
Then add HAVING COUNT(*) >= 2. Press Run it and step through each clause in the order SQLite works.
SELECT course_id, COUNT(*) AS passedFROM enrolmentsWHERE mark >= 60GROUP BY course_idHAVING COUNT(*) >= 2ORDER BY passed DESC;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
- FROM: 10 rows.
- WHERE keeps 7 rows. It drops Ben's 58 in Maths, Chloe's NULL in History and Dev's 47 in Physics.
- GROUP BY makes 4 bands: course 1 holds 72, 85 and 91; course 2 holds 65; course 3 holds 80; course 4 holds 90 and 76.
- HAVING keeps the groups with at least two rows: courses 1 and 4.
- SELECT gives one row per group: course 1 with 3 passes, course 4 with 2.
- ORDER BY sorts them by
passed, largest first. They are already in that order.
| WHERE | HAVING | |
|---|---|---|
| tests | one row | one group |
| runs | before GROUP BY | after GROUP BY |
| can use COUNT, AVG and the others | no | yes |
Run it. Then change >= 2 to >= 1 and see course 2 and course 3 come back.
When a test can go in either place, put it in WHERE. "Only courses 1 and 4" could be HAVING course_id IN (1, 4), but WHERE course_id IN (1, 4) drops the other rows before any grouping work is done.
Check yourself
You want the courses that have more than two students. The query starts SELECT course_id, COUNT(*) FROM enrolments. Which ending is right?
The course averages are 76.5, 56, 80 and 83 for courses 1 to 4. Which courses does this query give back?
SELECT course_id, AVG(mark)FROM enrolmentsGROUP BY course_idHAVING AVG(mark) >= 75ORDER BY course_id;Course 2, Physics, has two marks: 65 and 47. Why is it missing from the result of this query?
SELECT course_id, COUNT(*) AS passedFROM enrolmentsWHERE mark >= 60GROUP BY course_idHAVING COUNT(*) >= 2ORDER BY course_id;- Say why WHERE cannot test an average: it runs on single rows, before the groups exist.
- Keep only the groups that pass a test with HAVING, written after GROUP BY.
- Use WHERE and HAVING in one query: WHERE tests rows before grouping, HAVING tests groups after it.
- Put a test on a plain column in WHERE, so fewer rows reach the grouping.