Lesson 5 of 30 · Querying Basics
SQL LIKE, IN, BETWEEN and IS NULL: Patterns, Lists and Ranges
=, < and > take you a long way, but some questions come up so often that SQL has special operators for them: does a text match a pattern, is a value in a list, does it lie inside a range — and is a value missing altogether?
Patterns with LIKE
LIKE compares text with a pattern that can contain two wildcards:
%— any number of characters, including none_— exactly one character
SELECT name FROM items WHERE name LIKE 'Scroll%';
This finds Scroll of Fireball and Scroll of Return. '%Potion' matches names that end in "Potion", '%Bow%' matches names that contain "Bow" anywhere, and '_ira%' matches Kira Frostvale and Mira Sunstrider: one character, then "ira", then anything.
Patterns work on any text column. Dates in the joined column are text like 2025-01-12, so joined LIKE '2025%' finds every hero who joined in 2025. In SQLite, LIKE ignores upper and lower case for English letters ('scroll%' works too). NOT LIKE keeps the rows that don't match.
Lists with IN
IN checks whether a value is one of several options. It is a short way to write many ORs:
SELECT name, class FROM heroes WHERE class IN ('Mage', 'Healer');
That is the same as class = 'Mage' OR class = 'Healer'. NOT IN (...) keeps everything else.
Ranges with BETWEEN
BETWEEN low AND high checks a range, and it includes both ends: level BETWEEN 10 AND 15 matches levels 10, 11, … and 15. It works for text and dates too: joined BETWEEN '2025-01-01' AND '2025-03-31' finds the heroes who joined in the first quarter of 2025. Always write the smaller value first.
Missing values: IS NULL
Three heroes have no guild — their guild_id is NULL:
| id | name | class | level | gold | guild_id | joined |
|---|---|---|---|---|---|---|
| 4 | Dax Shadowstep | Rogue | 9 | 75 | NULL | 2025-03-30 |
| 5 | Elara Brightwell | Healer | 12 | 260 | 1 | 2025-02-14 |
| 6 | Finn Oakheart | Warrior | 5 | 40 | 4 | 2025-06-01 |
NULL means "no value" or "unknown" — it is not 0 and not an empty text. Any comparison with NULL, even NULL = NULL, gives NULL instead of true, and WHERE only keeps rows where the condition is true. That's why WHERE guild_id = NULL returns nothing. Use IS NULL and IS NOT NULL instead:
SELECT name, guild_id FROM heroes WHERE guild_id IS NULL;
This returns Dax Shadowstep, Iris Dawnpetal and Kira Frostvale.
The same trap hides in <> and NOT IN. WHERE guild_id <> 4 returns 9 heroes, not 12: the three heroes without a guild are dropped, because "is NULL different from 4?" has no true answer. When they belong in the result, say so: WHERE guild_id <> 4 OR guild_id IS NULL.
Common mistakes
= NULLinstead ofIS NULL. It is never true, so the query silently returns no rows.- Forgetting the wildcard.
name LIKE 'Scroll'only matches the exact text "Scroll" — write'Scroll%'. BETWEENthe wrong way round.BETWEEN 15 AND 10matches nothing.- Forgetting NULLs in a "not" filter.
<>andNOT INskip NULL rows; addOR column IS NULLif they should stay.
Assignment
The Night Market (guild 4) is looking for new recruits. Find every hero who:
- joined in 2025 — their
joineddate starts with2025, - has a level between 7 and 14, both included,
- is not already a Night Market member. Heroes with no guild (
guild_idis NULL) are welcome too.
Return the columns name, level and guild_id, in this order. The row order doesn't matter.
Keep learning
Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.
Lesson last updated on .