Lesson

MySQL Wildcards

10 min reading
Free Course

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 LIKE Patterns: If a user searches for _admin or 100% in an unescaped LIKE string, % and _ act as wildcards and match unintended rows. Escape backslash, %, and _ in your backend code before wrapping the search term in %...%.
  • Use REGEXP for Complex Patterns: When you need character sets or alternation, MySQL's REGEXP operator is cleaner and easier to read than chaining multiple LIKE conditions.
  • Avoid Leading Wildcards on Large Tables: Patterns starting with % force full table scans; switch to FULLTEXT indexes 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum