← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 5 of 8

LIKE & Wildcards

LIKE is like a search with wildcards, such as 'names that start with S'. The percent sign stands for any letters at all.
Syntax
sql
WHERE column_name LIKE 'pattern'
-- %  matches any number of characters
-- _  matches exactly one character

The Percent Wildcard

The LIKE operator is used to search for patterns in text columns, such as finding every email ending in a specific domain. We use the percent symbol as a wildcard. It represents zero, one, or multiple characters, so 'a%' matches anything starting with the letter a.

Example: The Percent Wildcard

sql
SELECT * FROM users WHERE email LIKE '%@gmail.com';

The Underscore Wildcard

The underscore character is another wildcard, useful when you know exactly how many characters to expect. It represents exactly one single character. This is perfect when you know the exact length of the pattern you want to match, like a 4-digit product code.

Example: The Underscore Wildcard

sql
SELECT * FROM products WHERE code LIKE 'A_23';

The NOT LIKE Operator

We can use NOT LIKE to find values that do not match our pattern, such as filtering out test accounts with a known naming convention. This helps exclude text with certain letters or domains from your results entirely.

Example: The NOT LIKE Operator

sql
SELECT * FROM users WHERE email NOT LIKE 'test%';

Case Sensitivity with LIKE

By default, the LIKE operator in MySQL is case-insensitive under the standard collation, so Apple and apple both match. If you need to search for an exact case match, you can use the BINARY operator before the search string to force a byte-for-byte comparison.

Example: Case Sensitivity with LIKE

sql
SELECT * FROM users WHERE name LIKE 'Apple'; -- matches 'apple' too, case-insensitive by default

Escaping Wildcards

Sometimes you want to search for the literal percentage or underscore characters instead of using them as wildcards, like matching a discount code that contains a real percent sign.

We can do this by using a custom escape character. This tells MySQL to treat them as normal letters rather than pattern symbols.

Example: Escaping Wildcards

sql
SELECT * FROM coupons WHERE code LIKE '50\%OFF' ESCAPE '\\';
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using = with wildcards, like email = '%@gmail.com', which compares literally and matches nothing.
  2. Confusing % (any number of characters) with _ (exactly one character).
  3. Starting a pattern with %, like '%gmail', on a large indexed column, which cannot use the index and is slow.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.