Learn SQL
-- filtering.like

LIKE — Pattern Matching in SQL

Match text against wildcards

--concept

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
idname1Alice2Bob3Cynthia4David5Elena6Farid% = any characters · _ = one character'A%'pattern applied to name column
pattern match no match
Stage 1 / 8
LIKE 'A%' — % matches ANY run of characters, _ matches exactly one letter.
--example
query.sql
1SELECT name, department
2FROM employees
3WHERE name LIKE '%n%';
Result
namedepartment
CynthiaEngineering
ElenaSales
FaridEngineering

%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
1SELECT name FROM employees
2WHERE name LIKE '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
1SELECT name FROM employees
2WHERE name LIKE '%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
1SELECT name FROM employees
2WHERE name LIKE '_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
1SELECT name FROM employees
2WHERE name NOT LIKE '%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 '\'
--use cases
Search-as-you-type boxes (prefix patterns keep indexes usable)
Finding codes and SKUs by partial number
Data cleaning: WHERE email NOT LIKE '%_@%._%' flags malformed rows
URL/routing rules and simple allowlists
--under the hood
'A%' can use a B-tree index; '%A%' cannot — it must inspect every value.
For real fuzzy search, engines offer trigram indexes (pg_trgm) or full-text search — LIKE is the simple tool, not the scalable one.

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

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.