NO. 03 · 7 exercises

3. Pattern Matching

LIKE, wildcards, and finding text you half-remember.

The lesson

Pattern Matching

"Show me the track called 'Nocturne No. 1'" is easy — you saw exact text matching in the last chapter. But real customers rarely know the whole name. They know the title starts with an N, or that "winter" is in there somewhere, or that the song ends with "train". For fuzzy memories like these, SQL has LIKE — the pattern-matching operator.

LIKE doesn't compare text with a fixed value; it compares text against a pattern, using wildcards for the parts you don't remember.

LIKE: matching a pattern

The star of the show is the % wildcard: it stands for any number of characters — including zero. Put it wherever the memory gets fuzzy.

PatternMeaningExample
'N%'starts with Ntitle LIKE 'N%'
'%winter%'contains "winter" anywheretitle LIKE '%winter%'
'%train'ends with "train"title LIKE '%train'

Let's see them in action. A customer who only remembers the first letter:

SELECT title FROM tracks WHERE title LIKE 'N%';

Six tracks start with N — Nocturne No. 1, Northern Sky, Neon District and friends. Move the wildcard to both sides and the word can sit anywhere:

SELECT title FROM tracks WHERE title LIKE '%winter%';

That finds Winterberries and Winter Light, whatever position "winter" holds in the name. And the ending is just the reverse of the starting pattern:

SELECT title FROM tracks WHERE title LIKE '%train';

The wildcard is in front, the remembered word is at the end. Nothing about LIKE checks word boundaries — %train matches "train" as the final letters, plain and simple.

Case: SQLite doesn't sweat the capitals

The catalog spells one track midnight train, all lowercase. But a customer heard "Midnight Train" — capital M — on the radio. Try the pattern with shouting capitals:

SELECT title FROM tracks WHERE title LIKE '%TRAIN';

It still matches. SQLite's LIKE is case-insensitive: 'train' and 'TRAIN' and 'TrAiN' are all the same pattern to it. This is a gift for the "I don't remember the exact spelling" problem — and a deliberate trap to remember that plain = is not case-insensitive. WHERE title = 'TRAIN' finds nothing; WHERE title LIKE '%TRAIN' finds the track. Two different tools, two different temperaments.

One honest caveat: SQLite's case-insensitivity covers only the letters A–Z. Accented letters play by strict rules — 'MARÉ' will not match maré (you'll deal with accents properly in a later chapter).

_ : exactly one character

% means any number. Its stricter cousin _ means exactly one character — no more, no less. It's for patterns where you remember the length.

A customer asks for the track "Nocturne No." followed by a digit they can't recall:

SELECT title FROM tracks WHERE title LIKE 'Nocturne No. _';

_ matches the 1, the 2, any single character — but only one. Nocturne No. 12 wouldn't match, and neither would Nocturne No. alone. The underscore counts every character, spaces included: 'S_udade' matches Saudade because _ covers exactly the second letter.

Think of it as % = "however many characters", _ = "this one character right here".

NOT LIKE

Every pattern can be flipped with NOT:

SELECT name FROM artists WHERE name NOT LIKE '%the%';

Every artist whose name does not contain "the" — nine of them, since The Amber Lines, The Delta Kings and The Paper Cranes are excluded. NOT LIKE is case-insensitive exactly like LIKE, so %the% catches all three "The..." bands. Combine them freely: WHERE title NOT LIKE 'N%' AND title NOT LIKE '%e' means starts with neither N nor ends with e.

The sneaky apostrophe

Text patterns use single quotes — but what if the text you're matching contains a single quote? One track is called Don't Stop, and its apostrophe would end the quoted string early and break the query:

-- wrong: the quote in Don't closes the string too early
SELECT title FROM tracks WHERE title LIKE 'Don't%';

The fix is to escape the apostrophe by doubling it. In SQL, '' inside a string means a literal apostrophe:

SELECT title FROM tracks WHERE title LIKE 'Don''t%';

Read Don''t% as: the letters D-o-n, a real apostrophe, the letter t, then any number of characters. The doubling rule applies anywhere you write text — in LIKE patterns, in = comparisons, everywhere.

Combining LIKE with other conditions

Patterns are just conditions, so they play with everything from the filtering chapter. AND, OR, parentheses, price checks — all mix freely:

SELECT title
FROM tracks
WHERE (title LIKE 'N%' OR title LIKE '%e')
  AND unit_price = 0.99;

The parentheses group the two patterns together; then the price filter applies to the whole group. This is exactly the shape of a real-world search: three vague clues, one precise result.

What you just learned

  • LIKE matches text against patterns; % means any number of characters, _ means exactly one.
  • 'N%' starts with, '%winter%' contains, '%train' ends with — position the wildcards around what you remember.
  • SQLite's LIKE is case-insensitive; plain = is not.
  • NOT LIKE keeps the rows that do not match.
  • An apostrophe inside a quoted string is escaped by doubling it: 'Don''t'.
  • Patterns combine with AND, OR and other conditions like any other filter.

Dialect note. This one really differs between databases. SQLite's LIKE is case-insensitive for A–Z. In PostgreSQL, LIKE is case-sensitive — you'd use ILIKE for the behavior above; MySQL's LIKE follows the collation of the column, which is often case-insensitive anyway. And if you want case-sensitive matching in SQLite itself, there's a cousin operator called GLOB that uses * and ? instead of % and _. Nice to know it exists; you won't need it here.

Half-remembered titles are no match for you now. The exercises below are exactly these five minutes of work — go find the tracks your customers can't name.