MySQL Wildcards

MySQL Tutorial

MySQL Wildcards

Wildcards in MySQL are special characters used with the LIKE operator to search for patterns within string values. They allow flexible and partial matching instead of exact matching.

Wildcards are mainly used in search queries, especially when you don’t know the exact value you’re searching for.


Wildcards Used with LIKE

WildcardDescription
%Matches zero, one, or many characters
_Matches exactly one character
[]Matches any single character within the bracket (used in SQL Server, not commonly in MySQL)
[^ ] or [! ]Matches any single character NOT in the bracket (rare in MySQL)

In MySQL, the most commonly used wildcards are % and _.


 Example Table: products

idnamecategory
1Apple iPhoneMobile
2Samsung GalaxyMobile
3Dell LaptopLaptop
4HP LaptopLaptop
5Apple WatchWatch

 Using % Wildcard

1. Starts With

Matches:

  • Apple iPhone

  • Apple Watch


 2. Ends With

Matches:

  • Dell Laptop

  • HP Laptop


 3. Contains

Matches:

  • Samsung Galaxy


 Using _ Wildcard (Single Character Match)

 Match a Name with Exactly 5 Characters

(5 underscores → exactly 5 characters)


 2nd Character Match

Matches words where 2nd letter is ‘a’, such as:

  • Samsung Galaxy

  • Apple Watch (no match here; 2nd letter = p)


 Combining % and _

Breakdown:

PatternMeaning
AMust start with A
_Any single character
p%Followed by p and anything after

Matches:

  • Apple iPhone (A p …)

  • Apple Watch


 Escape Characters (When Searching Wildcard Symbols)

If you want to search for actual characters like % or _, use ESCAPE:

This searches for names containing an actual underscore (_), not a wildcard.



 Summary Table

WildcardExampleMeaning
%'A%'Starts with A
%'%e'Ends with e
%'%top%'Contains “top”
_'A_p%'A + one character + p + rest
_ _ _'___'Exactly 3 characters


When to Use Wildcards

Use CaseExample
Searching unknown spelling%sung% → Samsung
Searching partial match%Laptop
Auto-suggest or search box%keyword%

You may also like...