What's a wildcard?
A plain comparison is exact: name = 'Aarav' finds Aarav and nothing else – one letter off, and it comes back empty. A wildcard is a stand-in character you drop into a search pattern when you only know the shape of the text, not the exact string – "starts with A," "has an 'an' in it somewhere," "is exactly five letters."
LIKE is the SQL keyword that turns a plain string comparison into that kind of shape-matching search. It only understands a couple of special characters to describe that shape – everything else in the pattern still has to match literally.
Every wildcard, at a glance
Percent – any run of characters
Matches zero or more characters of any kind, including none at all.
Underscore – exactly one
Matches exactly one character – never zero, never two.
Bracket list – not everywhere
Matches one character from a set. This is a SQL Server / MS Access extension – MySQL and PostgreSQL don't support it at all.
Escaping a literal
Need to match an actual % or _? Escape it and say so.
The two you'll use constantly
Of the four above, % and _ are the ones that work the same in every major database. Here's exactly what each one does, with real examples.
% – any run of characters, including none
'A%'– starts with A → Aarav, Ananya, Aditi, Arjun'%a'– ends with a → Ananya, Priya'%an%'– has "an" anywhere in the middle → Rohan, Simran
The part people forget: % can match zero characters too, so 'A%' would just as happily match the single letter "A" on its own as it matches "Aarav."
_ – exactly one character, never more or less
'_abir'– any first letter, then "abir" → Kabir'K_b_r'– K, any letter, b, any letter, r → Kabir
Each _ accounts for exactly one character position – line up five of them ('_____') and you've written "exactly five characters," full stop. Too short or too long, and it won't match no matter what the letters are.
Try it yourself
Type a pattern below – the names that match light up gold, computed live against the exact same rule MySQL and PostgreSQL use.
Equivalent regular expression:
Reading the pattern out loud
| Pattern | Reads as | Matches here |
|---|---|---|
| 'A%' | starts with A | Aarav, Ananya, Aditi, Arjun |
| '%a' | ends with a | Ananya, Priya |
| '%an%' | contains "an" anywhere | Rohan, Simran |
| '_abir' | 5 characters, ending "abir" | Kabir |
| 'K_b_r' | 5 characters, K_b_r with any 2nd and 4th letter | Kabir |
Case sensitivity depends on the column's collation, not on LIKE itself – MySQL's default collations are case-insensitive, so 'a%' and 'A%' usually match the same rows.
The index gotcha
A trailing wildcard – 'A%' – can still use a standard B-tree index, because the engine can seek straight to rows starting with "A." A leading wildcard – '%an%' – can't: every row has to be scanned, since a match could start anywhere. On a large table, that's the difference between an index seek and a full scan.
Want to run these patterns against a real table?
Open the SQL compiler →