Skip to content
Codewilds

Lesson 5 of 30 · Querying Basics

SQL LIKE, IN, BETWEEN and IS NULL: Patterns, Lists and Ranges

medium20 XP3 min readFree lesson

=, < 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

  • = NULL instead of IS 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%'.
  • BETWEEN the wrong way round. BETWEEN 15 AND 10 matches nothing.
  • Forgetting NULLs in a "not" filter. <> and NOT IN skip NULL rows; add OR column IS NULL if they should stay.

Assignment

The Night Market (guild 4) is looking for new recruits. Find every hero who:

  1. joined in 2025 — their joined date starts with 2025,
  2. has a level between 7 and 14, both included,
  3. is not already a Night Market member. Heroes with no guild (guild_id is 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 .

Write code ↓
SQL+20 XP

Press Run to execute your code, or Submit to check it against the tests.