MySQL Wildcards
MySQL Wildcards: %, _, and Pattern Escaping
MySQL wildcards are special placeholder characters used with the LIKE operator to match flexible text patterns. Knowing how %, _, and ESCAPE behave ensures your search filters match exact patterns—even when user input contains literal underscores or percent signs.
Supported LIKE Wildcard Characters
| Wildcard | Meaning | Example Pattern | Matches |
|---|---|---|---|
% |
Zero or more characters | 'Pro%' |
'Pro', 'Product', 'Pro Max' |
_ |
Exactly one character | 'SKU-_01' |
'SKU-A01', 'SKU-901' |
Unlike SQL Server, standard MySQL LIKE does not support [a-z] bracket character classes—use REGEXP when you need character ranges.
Practical SQL Examples
This query matches fixed-length SKU codes in demo_products and escapes a literal underscore when searching coupon descriptions:
-- Match SKUs starting with 'KB-' followed by exactly 3 characters
SELECT id, sku, product_name
FROM demo_products
WHERE sku LIKE 'KB-___';
-- Search for literal '20%' in promo descriptions using ESCAPE
SELECT id, product_name
FROM demo_products
WHERE product_name LIKE '%20!%%' ESCAPE '!';
| id | sku | product_name |
|---|---|---|
| 1 | KB-101 | Mechanical Keyboard |
| 4 | KB-204 | Wireless Keyboard |
Common Mistakes and Best Practices
- Escape User Input Before Building
LIKEPatterns: If a user searches for_adminor100%in an unescapedLIKEstring,%and_act as wildcards and match unintended rows. Escape backslash,%, and_in your backend code before wrapping the search term in%...%. - Use
REGEXPfor Complex Patterns: When you need character sets or alternation, MySQL'sREGEXPoperator is cleaner and easier to read than chaining multipleLIKEconditions. - Avoid Leading Wildcards on Large Tables: Patterns starting with
%force full table scans; switch toFULLTEXTindexes when searching large text columns.
Frequently Asked Questions
Why does LIKE '%_%' match almost every non-empty string?
Because _ is a single-character wildcard, '%_%' matches any string with at least one character unless you escape it as '%\__%' or use ESCAPE.
Does MySQL support [abc] wildcards in LIKE?
No, bracket character lists are specific to T-SQL (SQL Server); in MySQL, use REGEXP '[abc]'.
Can I change the default backslash escape character in LIKE?
Yes, append ESCAPE '!' (or another unused symbol) at the end of the LIKE expression.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.