Pattern Matching (LIKE, ILIKE, Wildcards % and _)
Search text with LIKE/ILIKE wildcards and know when indexes still help.
In plain words
% means "anything here" and _ means "exactly one letter here". LIKE checks whether text fits that shape.
Think of it like…
Looking up words in a dictionary: "words starting with pre" is a quick flip to the right page. "Words containing ord" means reading every page.
Where you’ve seen it
A product search: WHERE name ILIKE 'sams%' can use an index and is fast. WHERE name ILIKE '%galaxy%' on 5 million products is a full scan, so production search uses a trigram index (pg_trgm) or full-text search instead.
Words you’ll hear
- %
- Any number of characters, including zero.
- _
- Exactly one character.
- ILIKE
- Case-insensitive LIKE in PostgreSQL (MySQL depends on collation).
- ESCAPE
- Treat % or _ literally, as in LIKE '50\%%' ESCAPE '\'.
Visual diagram
Theory & example queries
Writing patterns
'Ra%' starts with Ra, '%son' ends with son, '%an%' contains an, '_a%' has 'a' as the second character, and '___' is exactly three characters. LIKE compares the whole string, so 'Ra' without wildcards behaves like equality.
Case sensitivity depends on the database: PostgreSQL LIKE is case-sensitive (use ILIKE or LOWER(col) LIKE 'ra%'), while MySQL with its default case-insensitive collations isn't. NOT LIKE excludes rows where the column is NULL, just like other comparisons. For more complex rules, regular expressions (~ in PostgreSQL, REGEXP in MySQL) go further.
| id | country | city | signup_date |
|---|---|---|---|
| 1 | IN | Pune | 2026-10-01 |
| 2 | IN | Delhi | 2026-10-02 |
| 3 | NULL | Mumbai | 2026-10-03 |
SELECT id, emailFROM usersWHERE email ILIKE '%+test@%' OR email LIKE 'qa\_%' ESCAPE '\';PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative matching accounts2 sample rows
| id | |
|---|---|
| 17 | asha+test@example.com |
| 28 | qa_automation@example.com |
Note: Sample rows satisfy the unescaped +test@ pattern or the literal qa_ prefix. LIKE/ILIKE case rules and matching sets are dialect- and collation-dependent.
The second pattern treats the underscore literally.
Key takeaway: % = anything, _ = one character; check your database's case rules.
Performance and safe user input
A B-tree index stores values sorted, so a fixed prefix ('sams%') becomes a range scan. In PostgreSQL with a non-C collation, the index must be created with text_pattern_ops (or varchar_pattern_ops) to support LIKE. A leading wildcard can't use it at all. For substring search, PostgreSQL's pg_trgm extension with a GIN index makes LIKE '%galaxy%' and ILIKE fast; for word-based search, full-text search is better.
When the pattern comes from a user, pass it as a parameter (never concatenate it into SQL), and escape their % and _ characters so they're matched literally. Otherwise a search for "50%" matches everything starting with 50.
CREATE EXTENSION IF NOT EXISTS pg_trgm;CREATE INDEX ON products USING gin (name gin_trgm_ops);PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative command responses2 sample rows
| statement | status |
|---|---|
| CREATE EXTENSION | CREATE EXTENSION |
| CREATE INDEX | CREATE INDEX |
Note: PostgreSQL returns these statuses on a successful setup. Whether a later %galaxy% query uses the GIN index depends on the planner, selectivity and table size.
Then WHERE name ILIKE '%galaxy%' can use the index.
Key takeaway: Prefix patterns use B-trees; substring patterns need trigram or full-text indexes.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Choose the pattern shape
Decide whether you need starts-with, ends-with or contains, and whether case should matter.
starts-with 'Ra%' · contains '%an%'When to use it & trade-offs
What it is
LIKE matches text against a pattern with two wildcards: % for any sequence of characters (including none) and _ for exactly one character. name LIKE 'Ra%' finds names starting with Ra. ILIKE is PostgreSQL's case-insensitive version. A pattern with a fixed prefix can use an index; a pattern starting with % can't use a normal one.
Why it matters: Search boxes, filters and data-cleaning queries all use LIKE, and its performance surprises people: '%term%' on a large table is a full scan. User input inside LIKE patterns also needs care, because a user typing % matches everything.
When to reach for it
- Autocomplete with starts-with patterns
- Filtering by email domain or code prefix
- Ad-hoc data exploration
When to avoid or be careful
- '%term%' on big tables without a trigram index
- Concatenating user input into LIKE patterns
- Relying on LIKE being case-insensitive across databases
LIKE vs regular expressions vs full-text search
Strengths: Simple, portable; prefix patterns can use B-trees.
Watch out: Leading wildcards scan unless you add a trigram index.
Strengths: Very expressive.
Watch out: Harder to read; usually can't use indexes.
Strengths: Stemming, ranking and fast GIN indexes.
Watch out: Matches words, not arbitrary substrings; more setup.
Interview signals & numbers to know
When this topic comes up
- “Search products by name”
- “Why is this LIKE query slow?”
- “Names starting with …”
- Case-insensitive search
- Autocomplete
Benchmarks & rules of thumb
- %
- zero or more characters
- _
- exactly one character
- pg_trgm
- extension for fast contains-search
Common mistakes & follow-ups
Common interview mistakes
- Expecting
LIKE '%term%'to use a normal index. - Forgetting that _ is a wildcard, so 'qa_%' also matches 'qax…'.
- Assuming LIKE ignores case in PostgreSQL the way it does in MySQL.
Likely interviewer follow-ups
- Why can LIKE 'abc%' use an index but LIKE '%abc' can't?
- How would you make a contains-search fast on millions of rows?
- How do you safely use user input in a LIKE pattern?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Pattern Matching (LIKE, ILIKE, Wildcards % and _) with hands-on SQL practice and interview Q&A.