SQL LIKE Operator: % and _ Wildcards for Text Search
= finds exact matches. LIKE finds text that fits a pattern: titles starting with "The", email addresses at a certain domain, product codes with a known prefix.
The two wildcards
%matches any sequence of characters, including none.'The %'means "starts with The and a space";'%data%'means "contains data anywhere"._matches exactly one character.'_a%'means "the second character is a", which is why Maya and Daniel match but Rosa does not.
Every other character in the pattern must match itself.
Expected output
title The Quiet Harbor The Last Lantern title Small Data, Big Ideas name Maya Lindqvist Daniel Okafor
Case sensitivity differs by database
In SQLite, LIKE ignores case for the 26 ASCII letters by default, so '%data%' finds "Small Data, Big Ideas". PostgreSQL's LIKE is case-sensitive and offers ILIKE for case-insensitive matching; MySQL's behavior depends on the column's collation. When portability matters, compare lower(title) LIKE '%data%', which works everywhere.
For case-sensitive patterns in SQLite, use GLOB, which uses Unix-style wildcards: * for any sequence and ? for one character, as in title GLOB '*Data*'.
Matching a literal % or _
To search for an actual percent sign or underscore, choose an escape character with ESCAPE: WHERE code LIKE '10\%%' ESCAPE '\' finds values that start with "10%".
Performance
A pattern that starts with a fixed prefix, like 'The %', can sometimes use an index. A pattern that starts with % cannot: the database must check every row. That is fine for small tables like ours. For searching words in large amounts of text, databases provide full-text search instead; SQLite's is called FTS5.
NOT LIKE
NOT LIKE keeps rows that do not match. Remember that rows where the column is NULL match neither LIKE nor NOT LIKE.
Common mistakes
- Forgetting the wildcards:
title LIKE 'Data'behaves like=and matches only the exact title "Data". - Using
*withLIKE, or%withGLOB. - Assuming case-insensitive matching works the same way in every database.
Summary
%matches any run of characters;_matches exactly one.- SQLite's
LIKEis case-insensitive for ASCII letters;GLOBis case-sensitive. - Leading
%patterns scan the whole table; use full-text search for big text.
Quiz: LIKE patterns