M.
Chinyuku Database Systems 2023
MySQL Practical 5- LIKE Operator and
Wild Cards
Pattern Matching
• SQL pattern matching enables you to use “_” to match any single character
• “% “to match an arbitrary number of characters (including zero characters).
• In MySQL, SQL patterns are case-insensitive by default.
• Use the LIKE or NOT LIKE comparison operators.
The LIKE operator and Wild Cards
• The basic operators (=, <, >) do not work well with text data.
• The LIKE operator is used in searches that involve patterns.
• Wildcards are used with the LIKE function to broaden search options.
• A wildcard is a character that can be substituted for zero or more strings.
Wildcard Function Example Comment
% Can be used before and after Like “%mango%” would SELECT * FROM Customer
the text being searched for return: mango, big s
to accept any number of mango, mango groove, WHERE City LIKE 'ha%';
characters before or after mangoes
the text.
_ Any single character Like “b_g” would return: SELECT * FROM Customer
bag, beg, big s
WHERE City LIKE '_arare';
# Any single digit Like ‘1#’ would return: SELECT * FROM Orders
10, 11, 12, 13,14, WHERE Quantity LIKE ‘1#’;
15,16,17,18,19
Exercise
Use the “exercise 4” database you created in practical 4 to do the exercise.
1. Find all customers whose first names begin with the letter ‘M’
SELECT * FROM customer WHERE FirstName LIKE 'M%';
Page 1|2
M. Chinyuku Database Systems 2023
2. Find all supplier names with exactly ten characters.
SELECT * FROM Supplier WHERE SupplierName LIKE '__________';
3. List all customer last names and addresses who are from the city whose name starts with
character ‘C’
SELECT LastName, Address
FROM Customer
WHERE City LIKE ‘C%’;
Page 2|2