Before you start, you need
Search for a pattern in a text
If you remember part of a name, you can search for it with LIKE. You do not need to know the full text: you just say which part is fixed and which part can vary. We call that rule a pattern.
% allows zero or more characters. 'M%' finds text that starts with M; '%27', text ending in 27; '%o%', text with an o anywhere.
_ allows exactly one character. The pattern 'M_' accepts Ma, but not M or May. On the other hand, 'M%' accepts all three, because what follows can be any length.
Write the pattern in single quotes after LIKE. Use = when you need to compare a complete text. In the challenge you will try short names to see the difference between % and _.
WHERE name LIKE 'M%'See it in action
We want product names that start with M.
SELECT name
FROM products
WHERE name LIKE 'M%';| name |
|---|
| Mouse |
| Monitor 24 |
| Monitor 27 |
- M fixes the start and % allows anything after it. Monitor 24 and Monitor 27 match the pattern.
- M% would also accept the text M on its own: the percent sign can stand for zero characters.
- To look for M followed by a single character, use 'M_'. The challenge checks that difference with M, Ma and May.
- The pattern is a text value and needs single quotes. Pattern matching does not understand what the words mean.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Find a letter inside the name
Write a query that returns the names of products that contain the letter 'o'. The order does not matter.
Expected result: 6 rows with the column name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The letter can be in any position in the name.
The full pattern is %o%.
Put the pattern in quotes.
Solution explained
SELECT name FROM products WHERE name LIKE '%o%';The percent signs allow characters before and after the o.
Look for an ending
Guided practice
Look for an ending
Show the name of products whose name ends in 27. Use LIKE.
Expected result: 1 row with the column name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Where should the 27 be: at the beginning, in the middle or at the end of the name?
The percent sign (%) stands for any number of characters, including none.
The pattern is %27: any start, ending in 27.
Solution explained
SELECT name FROM products WHERE name LIKE '%27';The percent sign allows any start, but 27 must be at the end.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Count one character
Show the name of products that match M followed by exactly one character. Use LIKE.
Expected result: 1 row with the column name, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
The number of characters is part of the requirement.
A wildcard accepts exactly one character.
The pattern starts with M and ends with _.
Solution explained
SELECT name FROM products WHERE name LIKE 'M_';Only Ma matches the pattern: M has no extra character and May has two.
Key takeaways
- % represents zero or more characters.
- _ represents exactly one character.
- The pattern is enclosed in single quotes.
Go deeper
How LIKE treats upper case and accents can vary between engines and settings. Check the behaviour with your application's text. The main examples do not depend on that difference.