Wildcards are used in SQL to match a string pattern. There are two types of wildcards:
% (percent sign) represents zero, one, or more characters.
_ (underscore) represents exactly one character.
Key Takeaway: SQL wildcards — % for multiple characters and _ for a single character — are used with the LIKE operator to search for patterns in string data, enabling flexible and powerful text filtering.
Wildcards are used with the LIKE operator in SQL. The application of wildcard in SQL will be shown in the SQL LIKE section.
Examples
Below are some wildcard examples:
'A_Z': All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters between A and Z instead of one).
'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition.
'%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition.
'%AN%': All strings that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition.
'_AN%': All strings that contain a character, then 'AN', followed by anything else. For example, 'SAN FRANCISCO' would satisfy the condition, while 'LOS ANGELES' would not satisfy the condition.
Exercises
1. Which of the following strings satisfies the condition 'T_'? (There can be more than one answer)
a) TW
b) TWITTER
c) TAIWAN
d) To
2. Which of the following strings satisfies the condition 'A_B%'? (There can be more than one answer)
a) AKKB
b) AKBK
c) ABKK
d) ABBB
3. Which of the following strings satisfies the condition 'Z%K_R'? (There can be more than one answer)
a) ZKRR
b) ZJJR
c) ZRKKJ
d) ZABCDEFKR
1. a), d)
2. b), d)
3. a)
Frequently Asked Questions
What are SQL wildcard characters?
SQL has two wildcard characters: % (percent), which matches zero, one, or more characters, and _ (underscore), which matches exactly one character. Both are used with the LIKE operator.
How do you use wildcards in a SQL query?
Wildcards are placed inside the pattern string of a LIKE operator. For example: WHERE Name LIKE 'A%' matches any name starting with "A".
What is the difference between % and _ wildcards?
% matches any number of characters (including zero), while _ matches exactly one character. So 'A_Z' matches 'ABZ' but not 'ABCZ', whereas 'A%Z' would match both.
Can wildcards be combined in a single pattern?
Yes. You can combine % and _ in the same pattern. For example, '_AN%' matches strings where the second and third characters are 'AN', such as 'SAN FRANCISCO'.