Sub topic 2
Matching Text Patterns with `LIKE`
Assume you know basic SELECT statements, tables, columns, comparison operators, and AND/OR. This chapter focuses on matching text patterns in a WHERE clause.
How `LIKE` works
LIKE compares a text expression with a pattern. The pattern can contain wildcard characters:
| Wildcard | Meaning | Example |
|---|---|---|
| `%` | Any sequence of zero or more characters | `'A%'` matches `Ana` and `A` |
| `_` | Exactly one character | `'AB_-%'` matches `ABX-100` |
A pattern without wildcards behaves much like equality, although = is clearer for an exact match.
Use % when you know only part of the text:
SELECT id, name
FROM employees
WHERE name LIKE 'A%';This returns names that start with A. The pattern 'A%' means that the first character must be A, followed by any number of characters, including none.
Wildcards can appear anywhere:
SELECT id, email
FROM customers
WHERE email LIKE '%@example.com';This finds addresses that end with @example.com. A pattern such as '%sql%' finds text containing sql.
The underscore matches one character exactly:
SELECT id, product_code
FROM products
WHERE product_code LIKE 'AB_-%';This matches ABX-100 because _ matches X. It does not match AB-100, because there is no character between AB and -.
Matching literal wildcard characters
Sometimes % or _ is part of the data rather than a wildcard. Use an escape character to treat the next character literally.
SELECT id, label
FROM promotions
WHERE label LIKE '10!%%' ESCAPE '!';Here, ! is the escape character. The first % is preceded by !, so it is literal. The second % remains a wildcard. The query matches labels that start with the text 10%.
You can also escape _:
SELECT id, code
FROM products
WHERE code LIKE 'A!_B%' ESCAPE '!';This matches values beginning with the literal text A_B.
The escape character is part of the SQL pattern syntax. It is not expected to appear in the matching data.
Case sensitivity and portability
Case sensitivity depends on the database, its collation, and sometimes the column definition. A query using LIKE 'a%' might match Alice on one system but not another.
For a portable case-insensitive approach, normalize the column and pattern to the same case:
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'a%';This usually matches both Alice and alice. LOWER() can prevent an ordinary index on name from being used unless the database has a matching functional index or an equivalent generated column. Some databases provide another operator or collation for case-insensitive matching, so check the rules for your database.
LIKE does not match NULL. A NULL value means the value is unknown, so the comparison does not become true:
SELECT id, name
FROM employees
WHERE name LIKE 'A%';Rows where name is NULL are excluded. To include them, state that requirement separately:
SELECT id, name
FROM employees
WHERE name IS NULL
OR name LIKE 'A%';Negating a pattern
Use NOT LIKE to reject matching text:
SELECT id, name
FROM employees
WHERE name NOT LIKE 'A%';Rows with NULL names are still excluded because NULL NOT LIKE 'A%' is unknown, not true. Add OR name IS NULL if unknown names should be included.
For performance, a pattern beginning with fixed text, such as 'A%', can often use an index on the column. A pattern beginning with %, such as '%sql%', usually cannot use an ordinary ordered index efficiently. Confirm the actual plan with your database's query-plan tool.
Exercise
Write a query that returns id and label for promotions whose label contains the literal text 20%.
Show answer
SELECT id, label FROM promotions WHERE label LIKE '%20!%%' ESCAPE '!';