Lesson 3 · Milestone: Filter

Odd numbers, NULL and ORDER BY

Three small tools close the Filter milestone: % to test for odd numbers, NULL (the value that breaks <>), and ORDER BY, the step that runs after SELECT.

What you will be able to do:

Four words you need

%Modulo: the remainder after division. 7 % 2 is 1. You can also write MOD(7, 2).
NULL"No value / unknown". It is not 0 and not ''.
UNKNOWNThe result of any comparison with NULL. WHERE treats it like FALSE.
DESCSort from biggest to smallest. The default, ASC, is smallest first.

1. Odd and even with %

id % 2 = 1    -- odd:  1, 3, 5, 7 …  (remainder 1)
id % 2 = 0    -- even: 2, 4, 6, 8 …  (remainder 0)

Remainders work for any "every N-th" question. For example, id % 3 = 0 keeps every third id.

2. NULL: the third answer

Most people think a test is TRUE or FALSE. With NULL there is a third answer: UNKNOWN. "Is an unknown genre different from 'horror'?" Nobody knows. MySQL says that comparing anything with =, < or <> to NULL gives NULL (MySQL manual).

WHERE keeps a row only when the test is TRUE.

  TRUE     →  kept
  FALSE    →  dropped
  UNKNOWN  →  dropped   ← this is the surprise

3. ORDER BY runs after SELECT

Without ORDER BY, the rows come back "in no particular order" (MySQL manual). If a question says "sort", you need ORDER BY. Here is the run order, with one more step added:

1. FROM  →  2. WHERE  →  3. SELECT  →  4. ORDER BY

Because ORDER BY runs after SELECT, it can use an alias from SELECT. WHERE can't, because it runs before the alias exists.

Click through the four steps. This example uses a different table:

SELECT title, score
FROM Books
WHERE id % 2 = 1 AND genre <> 'horror'
ORDER BY score DESC;
1 · FROM Books id title  genre   score 1  Dune   scifi   8.9 2  It     horror  7.1 3  Emma   NULL    7.8 4  Ubik   scifi   8.2 5  Hobbit fantasy 9.1 all 5 rows 2 · WHERE id title  test 1  Dune   TRUE    ✓ 2  It     FALSE 3  Emma   UNKNOWN 4  Ubik   FALSE 5  Hobbit TRUE    ✓ Emma: NULL <> 'horror' is UNKNOWN, so the row is dropped 3 · SELECT title  score Dune   8.9 Hobbit 9.1 order not guaranteed yet 4 · ORDER BY score DESC title  score Hobbit 9.1 Dune   8.9 biggest score first

FROM takes all 5 rows. Notice that Emma's genre is NULL: nobody entered it.

WHERE tests each row. Ids 2 and 4 are even, so the test is FALSE. Emma has an odd id, but NULL <> 'horror' is UNKNOWN, so the whole test is UNKNOWN and the row is dropped. Only Dune and Hobbit are TRUE.

SELECT keeps title and score. The rows have no guaranteed order at this point.

ORDER BY score DESC runs last and sorts biggest first. Hobbit (9.1) moves above Dune (8.9).

If you wanted Emma too, you would write AND (genre <> 'horror' OR genre IS NULL). The parentheses matter for the reason you saw in Lesson 2: AND runs before OR.

Practice from memory

Try not to scroll up.

1. What is 7 % 2?

2. A row has genre = NULL. The query has WHERE genre <> 'horror'. What happens?

3. Which test finds rows with no genre?

4. Which step runs after SELECT?

5. Can ORDER BY use an alias you made in SELECT?

6. What does ORDER BY score DESC do?

Fill in the blanks: a different table

Songs(id, title, mood, plays). (Both <> and != mean "not equal" in MySQL. Either is accepted.) Return all columns of songs with an even id whose mood is not 'sad', most-played first.

SELECT 
FROM Songs
WHERE id  2 = 
  AND mood  'sad'
ORDER BY plays ;

🏆 Milestone challenge: LeetCode 620 (Not Boring Movies)

Your task: open 620 and choose MySQL. The table is Cinema(id, movie, description, rating). Return the movies with an odd id and a description that is not 'boring'. Sort by rating, highest first. Time box: 7 minutes.

Send me in the chat:

To pass (check these yourself first):

Passing this unlocks the Filter milestone.

Read next

Main source: Working with NULL Values (MySQL 8.0 manual). It is one page long, and it shows that every comparison with NULL returns NULL.

💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.