LIKE & Wildcards (% and _)
Search text by pattern with LIKE, using % (any number of characters) and _ (exactly one character).
Sometimes you only know part of the text: "names starting with J", "names containing 'ar'". = needs the full exact value, so we use LIKE with wildcards.
Definition
LIKE tests whether a text value matches a pattern. A pattern is a string containing ordinary characters (which must match exactly) and wildcard characters (placeholders that stand for unknown characters):
%(percent) matches any sequence of zero, one or many characters._(underscore) matches exactly one single character.
LIKE must match the whole value: 'J%' means "J, then anything until the end". NOT LIKE keeps values that do not match.
Syntax
sql
SELECT column_list
FROM table_name
WHERE column_name LIKE 'pattern';
WHERE column_name NOT LIKE 'pattern';Pattern cheat sheet
| Pattern | Meaning | Matches in players.name |
|---|---|---|
'V%' | starts with V | Virat Kohli |
'%a' | ends with a | Rohit Sharma |
'%ar%' | contains "ar" anywhere | Rohit Sharma, David Warner |
'_o%' | 2nd letter is o | Rohit Sharma, Jos Buttler |
'J%' | starts with J | Jos Buttler, Jasprit Bumrah |
'% %' | contains a space | all 6 names |
'__ %' | exactly 2 characters, then a space | MS Dhoni |
Why does '%a' match only Rohit Sharma? The others end in i, r, i, r, h. And '%ar%' skips "Virat" because that has "ra", not "ar".
Example — names starting with J
sql
SELECT *
FROM players
WHERE name LIKE 'J%';| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
| 6 | Jasprit Bumrah | MI | India | 20 |
'J%' = starts with J, then anything. Jos and Jasprit match.
Example — names containing 'ar'
sql
SELECT *
FROM players
WHERE name LIKE '%ar%';| id | name | team | country | runs |
|---|---|---|---|---|
| 2 | Rohit Sharma | MI | India | 500 |
| 5 | David Warner | DC | Australia | 700 |
'%ar%' = "ar" anywhere in the name: Sharma and Warner. Because % also matches zero characters, '%ar%' would even match a name that is exactly "ar".
Example — _ for one character
sql
SELECT name
FROM players
WHERE name LIKE '_o%';| name |
|---|
| Rohit Sharma |
| Jos Buttler |
_ consumes exactly the first letter (R, J), then the 2nd must be "o", then % takes the rest.
Searching for a real % or _
What if the text itself contains % or _, like a promo code 50%OFF or IPL_2025? Use an ESCAPE character to say "treat the next character literally":
sql
WHERE code LIKE '%!%%' ESCAPE '!' -- contains a real %
WHERE code LIKE 'IPL!_2025' ESCAPE '!' -- a real underscoreIn SQL Server you can also wrap it in brackets: LIKE '%[%]%', LIKE 'IPL[_]2025'.
SQL Server extra wildcards
T-SQL LIKE supports two more wildcards (not in MySQL/PostgreSQL LIKE, not in this playground):
| Wildcard | Meaning | Example |
|---|---|---|
[ ] | any one character from a set or range | '[JV]%' → starts with J or V |
[^ ] | any one character not in the set | '[^A-M]%' → starts with N–Z |
Case sensitivity
| Database | Is LIKE 'v%' the same as LIKE 'V%'? |
|---|---|
| SQL Server | depends on collation — the default (CI = case-insensitive) says yes |
| MySQL | yes with default collations |
| PostgreSQL | no — LIKE is case-sensitive; use ILIKE for case-insensitive |
| This playground (SQLite) | yes for English letters |
Common mistakes
- ❌
WHERE name = 'J%'→=treats % as a normal character. ✅LIKE 'J%'. - ❌ Using
*or?(Windows/Access wildcards). ✅ SQL uses%and_. - ❌
LIKE 'J'expecting "starts with J" → without a wildcard it means exactly "J". ✅'J%'. - ❌ Assuming
'%a'= "contains a". ✅ it means ends with a; contains is'%a%'.
Performance note
LIKE 'J%' (fixed start) can use an index on name. A leading wildcard like LIKE '%ar%' forces SQL to check every row (a scan), which is slow on huge tables.
Interview tip
Q: What is the difference between % and _ in LIKE?
A: % matches any number of characters including zero; _ matches exactly one character. So '_o%' means "second letter is o".
Key points to remember
- LIKE matches text against a pattern; NOT LIKE keeps non-matching values.
- % matches any sequence of characters — zero, one or many.
- _ matches exactly one character: '_o%' means the 2nd letter is o.
- 'V%' starts with V, '%a' ends with a, '%ar%' contains ar.
- Without a wildcard, LIKE behaves like = (exact match).
- Use ESCAPE (or [%] / [_] in SQL Server) to search for a literal % or _.
- SQL Server also supports [abc] and [^abc]; case sensitivity depends on collation (default: insensitive).
Try it yourself
Starts with J / contains 'ar'
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
All four slide patterns
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Escaping a literal % and _
This example runs a small setup step first — click to see it
CREATE TABLE offers (code VARCHAR(20));
INSERT INTO offers VALUES ('FLAT50'), ('50%OFF'), ('IPL_2025'), ('IPLX2025');Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SQL Server bracket wildcards
-- Names starting with J or V
SELECT name
FROM players
WHERE name LIKE '[JV]%';
-- Result: Virat Kohli, Jos Buttler, Jasprit Bumrah
-- Names NOT starting with a letter from A to M
SELECT name
FROM players
WHERE name LIKE '[^A-M]%';
-- Result: Virat Kohli, Rohit SharmaSQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: names ending with 'r'
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
