Full answer
LIKE compares a value with a pattern that has two wildcards:
%matches zero or more characters.'Ma%'matches Maya and Ma._matches exactly one character.'R_vi'matches Ravi but not Rvi or Raavi.
CREATE TABLE names (n TEXT);
INSERT INTO names VALUES ('Ravi'),('Rvi'),('Raavi'),('Maya'),('Ma');
SELECT n,
n LIKE 'Ma%' AS starts_ma,
n LIKE 'R_vi' AS r_one_vi
FROM names
ORDER BY n;| n | starts_ma | r_one_vi |
|---|---|---|
| Ma | 1 | 0 |
| Maya | 1 | 0 |
| Raavi | 0 | 0 |
| Ravi | 0 | 1 |
| Rvi | 0 | 0 |
The trouble starts when the data itself contains % or _. A coupon table has notes like "50% off" and "500 points". Searching for '%50%' cannot tell the literal percent from the wildcard:
CREATE TABLE coupons (code TEXT, note TEXT);
INSERT INTO coupons VALUES ('A1','50% off'),('B2','500 points'),('C3','5 percent'),('NEW_USER','welcome'),('NEWXUSER','typo copy');
SELECT code, note FROM coupons WHERE note LIKE '%50%' ORDER BY code;| code | note |
|---|---|
| A1 | 50% off |
| B2 | 500 points |
Declare an escape character and put it in front of the character you want to match literally:
SELECT code, note FROM coupons WHERE note LIKE '%50\%%' ESCAPE '\' ORDER BY code;| code | note |
|---|---|
| A1 | 50% off |
Now \% means "a real percent sign". The same applies to underscores, which are common in codes such as NEW_USER:
SELECT code,
code LIKE 'NEW_%' AS plain,
code LIKE 'NEW\_%' ESCAPE '\' AS escaped
FROM coupons
WHERE code LIKE 'NEW%'
ORDER BY code;| code | plain | escaped |
|---|---|---|
| NEWXUSER | 1 | 0 |
| NEW_USER | 1 | 1 |
Without the escape, 'NEW_%' also matches NEWXUSER, because _ matched the X. PostgreSQL uses backslash as its default escape character for LIKE, so '%50\%%' works there even without the ESCAPE clause; SQLite has no default escape, so the clause is required.
When user input goes into a LIKE pattern, escape % and _ in the input as well as passing it as a parameter. Otherwise a user typing % turns a search into "match everything".
CREATE TABLE names (n TEXT);
INSERT INTO names VALUES ('Ravi'),('Rvi'),('Raavi'),('Maya'),('Ma');
SELECT n,
n LIKE 'Ma%' AS starts_ma,
n LIKE 'R_vi' AS r_one_vi
FROM names
ORDER BY n;
CREATE TABLE coupons (code TEXT, note TEXT);
INSERT INTO coupons VALUES ('A1','50% off'),('B2','500 points'),('C3','5 percent'),('NEW_USER','welcome'),('NEWXUSER','typo copy');
SELECT code, note FROM coupons WHERE note LIKE '%50%' ORDER BY code;
SELECT code, note FROM coupons WHERE note LIKE '%50\%%' ESCAPE '\' ORDER BY code;
SELECT code,
code LIKE 'NEW_%' AS plain,
code LIKE 'NEW\_%' ESCAPE '\' AS escaped
FROM coupons
WHERE code LIKE 'NEW%'
ORDER BY code;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- % matches any run of characters; _ matches exactly one
- ESCAPE declares a character that makes the next wildcard literal
- SQLite needs an explicit ESCAPE clause
- Escape wildcards in user search input, not only quotes
Common mistakes
- Searching for codes with underscores and getting extra matches
- Passing raw user input into a LIKE pattern
Follow-ups
- Is LIKE case-sensitive on SQLite and PostgreSQL?
- Which LIKE patterns can use a B-tree index?
Tips
- Show a pattern with _ catching an extra row; it proves you understand the wildcard