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:
- Keep only odd or even rows with
%. - Predict what
WHEREdoes with aNULL, and fix it withIS NULL. - Sort results with
ORDER BY … DESC, and say where it runs. - Solve LeetCode 620 alone.
Four words you need
7 % 2 is 1. You can also write MOD(7, 2).''.WHERE treats it like FALSE.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
- To test for NULL, use
IS NULLorIS NOT NULL.= NULLis never TRUE. - To keep rows with a missing genre, say so:
(genre <> 'horror' OR genre IS NULL). - Interviewers love this. When you see
<>orNOT IN, ask out loud: "Can this column be NULL?"
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;
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:
- Your query, and the LeetCode result.
- Two full sentences for an interviewer:
- Where does
ORDER BYrun in the run order, and what does that let it do thatWHEREcan't? - If one movie had
description = NULL, would your query return it? Why? How would you change the query if the business wanted it included?
- Where does
To pass (check these yourself first):
- LeetCode says Accepted.
- The odd test uses
%orMOD, and the sort usesDESC. - Sentence 1 says that ORDER BY runs after SELECT, and names one thing that allows (such as using an alias).
- Sentence 2 says that the row is dropped because the comparison is UNKNOWN, and gives a fix with
IS NULLand parentheses.
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.
- Working with NULL Values 📄 Docs · 3 min read · Look at the
1 <> NULLtable. That one result explains the whole trap. - Sorting Rows 📄 Docs · 3 min read · ASC/DESC and sorting by two columns. Note that DESC applies only to the column right before it.
- SQL queries don't start with SELECT 📄 Article · 5 min read · Julia Evans. Look again: ORDER BY is near the end of her diagram.
💬 Is something still unclear? Ask me in the chat. I'm your tutor for this course.