Sub topic 9
Matching Text Patterns with `LIKE`
This chapter assumes you know how tables, rows, columns, SELECT, and WHERE work. It also assumes an employees table with text columns such as first_name and last_name.
Exact comparison works when you know the complete value:
SELECT first_name, last_name
FROM employees
WHERE last_name = 'McCarthy';Often, however, you need to search for a pattern. SQL provides the LIKE operator for this purpose. LIKE compares text with wildcard characters that represent unknown text.
Using `%` for any number of characters
The percent sign (%) matches zero or more characters. For example, this query finds last names that begin with Mc:
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE 'Mc%';This can match McCarthy, McDonald, and Mc. The pattern does not require characters after Mc.
A percent sign at the beginning finds values that end with the specified text:
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%son';This can match Johnson, Wilson, and Anderson.
A percent sign on both sides finds the text anywhere in the value:
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%smith%';This can match Smith, Blacksmith, and Smithson.
Using `_` for one character
The underscore (_) matches exactly one character. This query finds four-letter first names beginning with Ann:
SELECT first_name
FROM employees
WHERE first_name LIKE 'Ann_';It can match Anna or Anny, but not Ann or Annette.
You can combine wildcards. The following pattern finds values beginning with A, containing at least one more character, and ending in n:
SELECT first_name
FROM employees
WHERE first_name LIKE 'A%n';Excluding patterns with `NOT LIKE`
Use NOT LIKE to exclude matching text:
SELECT first_name, last_name
FROM employees
WHERE last_name NOT LIKE 'Mc%';This returns rows whose last name does not begin with Mc. A row with a NULL last name does not satisfy this condition. NULL represents a missing or unknown value, and comparisons involving it are not true.
Case sensitivity and escaping
Case sensitivity depends on the database system and its collation settings. PostgreSQL usually treats LIKE as case-sensitive. PostgreSQL also provides ILIKE for case-insensitive matching:
SELECT first_name, last_name
FROM employees
WHERE last_name ILIKE 'mc%';MySQL and SQLite can use different case rules depending on configuration. Test the behavior in your database rather than assuming all systems match the same way.
The characters % and _ have special meanings. To search for one of these characters literally, use an escape character:
SELECT product_name
FROM products
WHERE product_name LIKE '%\%%' ESCAPE '\';Here, \% means a literal percent sign. The surrounding percent signs still mean “any text before or after it.”
How a pattern is evaluated
The database evaluates the pattern separately for each row selected by the query. Use AND, OR, and parentheses when a pattern is part of a larger condition.
SELECT first_name, last_name, department
FROM employees
WHERE (last_name LIKE 'Mc%' OR last_name LIKE 'O%')
AND department = 'Sales';Exercise
Write a query that returns first_name and last_name for employees whose first name ends with the letter a and whose last name does not begin with Z.
Show answer
SELECT firstname, lastname FROM employees WHERE firstname LIKE '%a' AND lastname NOT LIKE 'Z%';