MySQL Wildcards
Wildcards combined with the LIKE operator let you search for patterns within text instead of requiring an exact match.
What Are Wildcards?
A wildcard is a special character that stands in for one or more unknown characters within a string. MySQL wildcards only work when paired with the LIKE (or NOT LIKE) operator — using them with a plain = comparison treats them as literal characters instead of pattern placeholders.
The Percent Sign (%)
The percent sign matches zero, one, or any number of characters. It is by far the most commonly used wildcard. Placing % at the end of a pattern searches for strings that start with a given prefix; placing it at the start searches for a suffix; placing it on both sides searches for a substring anywhere in the value.
The Underscore (_)
The underscore matches exactly one character, no more and no less. It is useful when you know the length of the pattern but not every character — for example, matching a four-character product code where only the first letter is known.
Character Lists
Standard SQL supports bracketed character lists like [a-f] in some dialects, but MySQL's LIKE does not support this syntax natively — brackets are treated as literal characters. To match a character range or set in MySQL, use REGEXP (or RLIKE) instead, which supports full regular expression patterns including [abc] and [a-z].
- % — matches any number of characters, including none
- _ — matches exactly one character
- REGEXP '[a-z]' — matches a character class (use this instead of bracket wildcards)
- ESCAPE 'char' — lets you search for a literal % or _ inside a value
Find customers whose name starts with 'Jo'
SELECT customer_name
FROM customers
WHERE customer_name LIKE 'Jo%';Find emails hosted on any 'gmail' domain
SELECT customer_name, email
FROM customers
WHERE email LIKE '%@gmail.%';Match a five-character code using underscores
SELECT product_code, product_name
FROM products
WHERE product_code LIKE 'X___9';Exercise: MySQL Wildcards
What does the % wildcard match in a LIKE pattern?