Counting carefully
After this: Tell COUNT(*) from COUNT(mark), see AVG skip NULL, and avoid the whole-number division trap.
On this page · 7 sections · 3 checks
In lesson 1 the average mark was 73.77777777777777. Ten enrolments, marks adding up to 664. But 664 divided by 10 is 66.4, not 73.8. Where did the difference come from? From the one row with no mark, and from how SQL counts. This lesson is about getting the count right.
The missing mark
Chloe takes History, but her mark is not in yet. Her row holds in the mark column. NULL means "no value here". It is not 0: Chloe did not score zero, she has no mark yet.
So the head teacher's question "how many?" has two answers. How many enrolments are there? 10. How many marks are there? 9.
SELECT COUNT(*), COUNT(mark)FROM enrolments;| COUNT(*) | COUNT(mark) |
|---|---|
| 10 | 9 |
COUNT(*)counts rows. It does not look inside them, so the NULL row counts: 10.COUNT(mark)counts the values in themarkcolumn that are not NULL: 9.
The rule: COUNT(column) skips NULL, and so do SUM, AVG, MIN and MAX. Only COUNT(*) counts every row.
The average skips NULL too
Work out AVG by hand:
- Add up the marks that are there: 664. NULL is skipped.
- Count the marks that are there: 9.
- Divide: 664 / 9 = 73.78 (to two places).
That is why the average is 73.8 and not 66.4. Leaving the missing mark out is the right answer here. If NULL counted as 0, Chloe's unmarked course would pull the class average down by more than seven marks.
When there are no rows
Nobody takes Music, which is course 5. Ask for its count and its average:
SELECT COUNT(*), AVG(mark)FROM enrolmentsWHERE course_id = 5;| COUNT(*) | AVG(mark) |
|---|---|
| 0 | NULL |
WHERE leaves no rows. The count of no rows is 0. The average of no marks has no answer, so AVG gives NULL. SUM, MIN and MAX of no rows give NULL too; only COUNT gives 0. The query still gives back one row, as lesson 1 promised.
Counting different values
COUNT(city) counts six cities, one per student. To count how many different cities there are, put DISTINCT inside the brackets, as you did after SELECT in the last tutorial.
SELECT COUNT(city), COUNT(DISTINCT city)FROM students;| COUNT(city) | COUNT(DISTINCT city) |
|---|---|
| 6 | 3 |
Whole-number division
Suppose you work out the average yourself, as the sum divided by the count:
SELECT SUM(mark) / COUNT(mark)FROM enrolments;| SUM(mark) / COUNT(mark) |
|---|
| 73 |
The answer is 73, not 73.78. Both 664 and 9 are whole numbers, and in SQLite one whole number divided by another gives a whole number: the part after the point is dropped. This is called . Java's / works the same way on two int values, and so does Python's // for positive numbers.
SELECT 7 / 2, 7 / 2.0, 7 % 2;| 7 / 2 | 7 / 2.0 | 7 % 2 |
|---|---|---|
| 3 | 3.5 | 1 |
7 / 2is 3: two whole numbers, so a whole answer.7 / 2.0is 3.5: one of them has a decimal point, so the answer keeps its decimals.7 % 2is 1: what is left over after dividing.
To keep the decimals, make one side a decimal number before dividing. Multiplying by 1.0 does that and does not change the value:
SELECT SUM(mark) * 1.0 / COUNT(mark)FROM enrolments;| SUM(mark) * 1.0 / COUNT(mark) |
|---|
| 73.77777777777777 |
Or use AVG(mark), which always gives a decimal answer.
Run it
All four counts side by side. Change COUNT(mark) to COUNT(student_id): no student id is NULL, so it gives 10.
Check yourself
Enrolments has 10 rows, and one of them has a NULL mark. What does this query give back?
SELECT COUNT(*), COUNT(mark)FROM enrolments;The nine marks add up to 664. What does this query give back in SQLite?
SELECT SUM(mark) / COUNT(mark)FROM enrolments;Course 5 is Music, and nobody takes it, so no enrolment has course_id 5. What does this query give back?
SELECT COUNT(*), AVG(mark)FROM enrolmentsWHERE course_id = 5;- Tell COUNT(*), which counts every row, from COUNT(mark), which skips the rows where mark is NULL.
- Say that SUM, AVG, MIN and MAX skip NULL too, so the average of 10 rows with one missing mark divides by 9.
- Say what the five functions give for no rows: COUNT gives 0, the others give NULL.
- Count different values with COUNT(DISTINCT city).
- Spot whole-number division (664 / 9 gives 73 in SQLite) and keep the decimals by multiplying by 1.0 first or by using AVG.