Function or operator · Filters and conditions

LIKE

LIKE is used inside WHERE to search for a specific pattern in text columns: % stands for any number of characters and _ stands for exactly one.

LIKE example

SQL
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.

Self-contained query · SQLite
WITH products(name) AS (
  VALUES ('Monitor 24'), ('Monitor 27'), ('USB Mouse')
)
SELECT name FROM products
WHERE name LIKE 'Monitor %' ORDER BY name;
Query result
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

PatternMeaningExample that matches
'Mon%'Starts with MonMonitor 24
'%USB%'Contains USBUSB-C Cable
'Monitor __'Monitor, a space and two charactersMonitor 24
'%o'Ends in oMouse 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.