In SQL, the LIKE operator searches for a specified pattern in a column inside a WHERE clause. It’s a very common way to query in practice, and most people only use the % wildcard — but several other patterns are supported too.
% Matches Any String of Zero or More Characters
- To find rows containing “work”:
LIKE '%work%' - To find rows starting with “Hebei Province”:
LIKE 'Hebei Province%' - To find rows ending with “community”:
LIKE '%community'
_ (Underscore) Matches Any Single Character
- For administrative division codes, to find provinces and municipalities nationwide:
LIKE '__0000' - To find cities under Hebei Province:
LIKE '13__00' - To find districts and counties under Zhangjiakou, Hebei:
LIKE '1307__'
[ ] Matches Any Single Character Within the Specified Range ([a-f]) or Set ([abcdef])
[CK]ars[eo]n matches: Carsen, Karsen, Carson and Karson (such as Carson)
[M-Z]inger matches all names ending in “inger” that begin with any single letter from M to Z (such as Ringer)
[^] Matches Any Single Character Not Within the Specified Range ([a-f]) or Set ([abcdef])
M[^c]% matches all names starting with the letter M whose second letter isn’t c (such as MacFeather)
# Stands for a Single Digit
This is similar to the underscore, except the underscore matches any single character while # matches digits only.
