LIKE and ILIKE
Objectives
By the end of this lesson, you should be able to:
- Match text by pattern with
LIKE - Use
%and_wildcards correctly - Use
ILIKEfor case-insensitive matching
💡 Why this matters: Not every text search is an exact match. “Names starting with J,” “emails containing ’example’,” these need pattern matching, not
=.
⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the
employeestable from earlier in this module.
LIKE and the % Wildcard
SELECT first_name FROM employees WHERE first_name LIKE 'J%';
first_name
------------
Jordan
% matches any sequence of characters, including none at all. 'J%' means “starts with J, followed by anything.” Put % on both sides to match a substring anywhere in the text:
SELECT first_name FROM employees WHERE first_name LIKE '%or%';
first_name
------------
Jordan
Taylor
Both Jordan and Taylor contain or somewhere in the middle. LIKE is case-sensitive by default, '%OR%' would match neither of these.
The _ Wildcard
_ (a single underscore) matches exactly one character, where % matches any number of them. '_a%' matches any name with a as its second letter, Maya, Sam, and Taylor all qualify, Erin (second letter r) doesn’t.
ILIKE: Case-Insensitive Matching
SELECT first_name FROM employees WHERE first_name ILIKE 'ERIN';
first_name
------------
Erin
ILIKE is PostgreSQL’s case-insensitive version of LIKE, everything else about wildcards works identically. 'erin', 'ERIN', and 'Erin' all match the same rows with ILIKE 'erin', useful for search features where users shouldn’t need to match capitalization exactly. Plain LIKE is a PostgreSQL-specific… actually LIKE is standard SQL, ILIKE specifically is a PostgreSQL extension, not every RDBMS (Module 1) has it, some rely on collation settings instead for case-insensitive matching.
Try It
- Write a query for every employee whose
first_namestarts withM. - Write a query for every employee whose
emailcontains'castillo'anywhere in it. - Write a query using
ILIKEthat matches'devon'regardless of how it was capitalized in the actual data. - Explain, in your own words, the difference between the
%and_wildcards.
Recap
LIKEmatches text by pattern,%matches any sequence of characters,_matches exactly one.LIKEis case-sensitive,ILIKE(a PostgreSQL extension) is the case-insensitive equivalent.- Wildcards can go anywhere in the pattern, at the start, the end, both, or the middle.
Next lesson: IS NULL, the one comparison ordinary operators can’t make.