LIKE compares text against a pattern containing wildcards: % matches any sequence of characters (including none), _ matches exactly one. WHERE name LIKE 'A%' finds names starting with A; '%son%' finds “son” anywhere; '_a%' finds names whose second letter is a. NOT LIKE inverts the match.
--how it works
1
Two wildcards, that is the language
% = zero or more characters; _ = exactly one. Everything else in the pattern must match literally.
2
Anchor or let it float
'A%' pins the A to the start; '%n' to the end; '%n%' lets it sit anywhere.
3
Case sensitivity is engine-specific
PostgreSQL LIKE is case-sensitive (ILIKE ignores case); MySQL defaults to case-insensitive.
4
NOT LIKE inverts
Keeps rows that fail the pattern — "everyone whose name does not start with A".
5
Escape literal wildcards
To match a real % or _, declare an escape character: LIKE '50\!%' ESCAPE '!'
--visualization
pattern match no match
Stage 1 / 8
LIKE 'A%' — % matches ANY run of characters, _ matches exactly one letter.
--example
query.sql
1SELECTname,department
2FROMemployees
3WHEREnameLIKE'%n%';
Result
name
department
Cynthia
Engineering
Elena
Sales
Farid
Engineering
%n% means “an n anywhere in the name” → Cynthia and Elena qualify (Farid has no n). Switch the pattern in the visualization to 'A%' or '_o%' and watch which names light up.
--variants
Prefix match — A%
Starts with A: the % floats, the A anchors.
query.sql
1SELECTnameFROMemployees
2WHEREnameLIKE'A%';
Result
name
Alice
1Only Alice begins with capital A.
2The pattern has no leading %, so an index can still serve it.
3Change to 'a%' on a case-sensitive engine and the result is empty.
Contains — %n%
The letter appears anywhere in the name.
query.sql
1SELECTnameFROMemployees
2WHEREnameLIKE'%n%';
Result
name
Cynthia
Elena
1Cynthia and Elena contain an n; Alice, Bob and David do not.
2The wildcard can match any position — first, middle or last.
3Leading % disables ordinary indexes — full scan territory.
One character — _
_ stands in for exactly one character.
query.sql
1SELECTnameFROMemployees
2WHEREnameLIKE'_o_';
Result
name
Bob
1Three letters, middle one o → only Bob qualifies.
2Bob: B-o-b ✓. No other name fits the 3-letter frame.
3Add letters by adding underscores: '____' matches any 4-letter name.
NOT LIKE
Everyone who fails the pattern.
query.sql
1SELECTnameFROMemployees
2WHEREnameNOTLIKE'%o%';
Result
name
Alice
Cynthia
David
Elena
Farid
1Only Bob contains an o, so only Bob is excluded.
2The other five pass NOT LIKE.
3Remember NULL names: they fail both LIKE and NOT LIKE and vanish.
--key facts
Two wildcards% (many) and _ (exactly one) — that is the whole language
Leading %a '%x' pattern cannot use ordinary indexes — full-scan territory
Escape literalsmatch a real % with ESCAPE, e.g. LIKE '50\%%' ESCAPE '\'
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free →Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →