The missing value
After this: Find NULL with IS NULL, explain why mark != 72 gives 8 rows and not 9, and say where NULL sorts.
On this page · 6 sections · 3 checks
Chloe takes History, but her teacher has not marked her work yet. What should the mark column hold for that row? Not 0: a 0 would say that Chloe scored nothing, which is not true. The honest answer is "no mark yet".
A marker for "no value"
SQL has a marker for exactly this: . It means there is no value in this cell. It is not 0, and it is not empty text. The tables on this page show it as a grey NULL:
SELECT student_id, course_id, markFROM enrolmentsWHERE student_id = 3;| student_id | course_id | mark |
|---|---|---|
| 3 | 1 | 85 |
| 3 | 3 | NULL |
It plays the same part as the missing marker you know from programming: None in Python, null in Java.
Looking for the missing mark
To find the unmarked row, the first idea is = NULL. It finds nothing:
SELECT student_id, course_idFROM enrolmentsWHERE mark = NULL;| student_id | course_id |
|---|
no rows
Here is why. A comparison with NULL is neither true nor false. SQL calls the answer , and the database shows it as NULL too:
SELECT NULL = NULL, NULL != 72, 72 != 72;| NULL = NULL | NULL != 72 | 72 != 72 |
|---|---|---|
| NULL | NULL | 0 |
Even NULL = NULL is unknown: two missing marks are not known to be equal. WHERE keeps a row only when its test is true, so unknown rows are left out, the same as false ones.
The test for a missing value is IS NULL, and its opposite is IS NOT NULL. They give true or false, never unknown:
SELECT student_id, course_idFROM enrolmentsWHERE mark IS NULL;| student_id | course_id |
|---|---|
| 3 | 3 |
WHERE mark IS NOT NULL gives the other 9 rows.
The surprise: 8, not 9
The enrolments table has 10 rows, and one of them has the mark 72. How many rows have a mark that is not 72? Most people say 9. The database says 8:
SELECT student_id, course_id, markFROM enrolmentsWHERE mark != 72;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
By hand, going through the 10 rows:
- Ana's Maths row has the mark 72.
72 != 72is false, so the row is left out. - Chloe's History row has no mark.
NULL != 72is unknown, so the row is left out too. - The other 8 rows have marks that are not 72. Each test is true, and they are kept.
The rule: a test on a NULL value is unknown, and WHERE drops it, whichever comparison you use. If you want the unmarked row as well, ask for it by name:
SELECT student_id, course_id, markFROM enrolmentsWHERE mark != 72 OR mark IS NULL;| student_id | course_id | mark |
|---|---|---|
| 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 |
NULL in a sorted list
When SQLite sorts, it treats NULL as smaller than any value. So in ascending order the unmarked row comes first:
SELECT student_id, course_id, markFROM enrolmentsORDER BY markLIMIT 3;| student_id | course_id | mark |
|---|---|---|
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 2 | 1 | 58 |
In descending order it comes last, as you saw at the ORDER BY step in lesson 3.
Run it
Press Run. Then try = NULL in place of IS NULL, and IS NOT NULL, and compare the rows you get.
Check yourself
Chloe's History mark is NULL. Which condition finds her row in the enrolments table?
The enrolments table has 10 rows. One has the mark 90, and one has no mark (NULL). How many rows does this query give back?
SELECT student_id, course_id, markFROM enrolmentsWHERE mark != 90;The marks are 72, 65, 80, 58, 90, 85, NULL, 47, 76 and 91. Which rows does this query give back?
SELECT student_id, course_id, markFROM enrolmentsWHERE mark < 60;- Say what NULL is: the marker for a missing value, which is not 0 and not empty text.
- Say that any comparison with NULL, even NULL = NULL, is unknown, and that WHERE drops unknown rows.
- Find missing values with IS NULL and the others with IS NOT NULL.
- Explain why WHERE mark != 72 gives 8 rows and not 9, and add OR mark IS NULL to keep the NULL row.
- Say that SQLite sorts NULL first in ascending order and last in descending order.