LIKE example
SELECT name, price
FROM products
WHERE name LIKE 'Monitor %'
ORDER BY name;The space after «Monitor» is part of the pattern. The % allows anything to follow, including nothing at all.
Check the result with small data
Two names start with «Monitor » and one does not.
WITH products(name) AS (
VALUES ('Monitor 24'), ('Monitor 27'), ('USB Mouse')
)
SELECT name FROM products
WHERE name LIKE 'Monitor %' ORDER BY name;| name |
|---|
| Monitor 24 |
| Monitor 27 |
The % symbol stands for any sequence of characters, even an empty one. This pattern also requires the space after Monitor, so it would not match «Monitors».
Check your understanding: Which pattern would find names ending in USB?
Show answer
'%USB': the leading % allows any beginning and USB must be the end.
What changes from case to case
How to read the patterns
| Pattern | Meaning | Example that matches |
|---|---|---|
'Mon%' | Starts with Mon | Monitor 24 |
'%USB%' | Contains USB | USB-C Cable |
'Monitor __' | Monitor, a space and two characters | Monitor 24 |
'%o' | Ends in o | Mouse does not match; neither does Speaker |
Predict where the wildcards sit. A % at the start stops many indexes from narrowing the search with a fixed prefix.
Upper case, lower case and collations
Do not assume that LIKE 'monitor%' will always find «Monitor 24». Case sensitivity depends on the engine and its comparison settings. PostgreSQL offers ILIKE as an extension for case-insensitive matching; SQLite compares ASCII letters case-insensitively by default, but not all of Unicode.
When the application needs a specific linguistic rule, define and test the collation or normalisation strategy on the real engine.
Searching for the % and _ characters themselves
If the data can contain the wildcard characters themselves, you need to escape them. Check the exact syntax and escape character for your engine. Do not concatenate text typed by a user straight into the pattern: use parameters and decide whether those characters should act as wildcards or as literal text.