Tier 3 · Intermediate

Pattern Matching (LIKE, ILIKE, SIMILAR TO, Regex) in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

What is Pattern Matching in SQL?

You already know LIKE for pattern matching with % wildcards. ILIKE and the POSIX regex operators extend that idea — each handles more complex matching requirements.

You're filtering a customer table for email addresses from .com domains, or pulling product codes that follow a specific format like SKU-0042-B. LIKE '%.com' handles the first case. Validating a format with exactly four digits between two dashes needs something more expressive. That's what the regex operators are for.

How do you match text case-insensitively in PostgreSQL?

ILIKE is the simplest extension — it's LIKE with case-insensitive matching. The pattern syntax is identical: % for any sequence of characters, _ for exactly one character. The only difference is that the match ignores case:

SELECT name, email
FROM customers
WHERE email ILIKE '%.com'

ILIKE '%.com' matches [email protected], [email protected], and anything in between. Use it when casing in your data is inconsistent.

How do you use a regex in a PostgreSQL WHERE clause?

For more complex patterns, PostgreSQL has POSIX regex operators: ~ (case-sensitive match), ~* (case-insensitive match), !~ (no match, case-sensitive), !~* (no match, case-insensitive). Unlike LIKE, these are not anchored by default — the pattern matches if it appears anywhere in the string. Use ^ to anchor at the start and $ at the end:

SELECT name, code
FROM products
WHERE code ~ '^SKU-[0-9]{4}-[A-Z]$'

This matches codes that start with SKU-, followed by exactly four digits, a dash, and one uppercase letter. The regex syntax is the same dialect used in Python and most command-line tools, so it carries over directly.

What is SIMILAR TO in PostgreSQL?

SIMILAR TO exists as a middle ground between LIKE and full regex. It adds alternation (|), repetition (+, *), and grouping (()) to LIKE-style patterns. It also anchors the pattern at both ends by default, so SIMILAR TO 'hello' matches only the exact string 'hello', and you need SIMILAR TO '%hello%' to match it anywhere. In practice, most analysts skip SIMILAR TO. The POSIX operators already cover everything it can do with a more familiar syntax. You'll see it occasionally in existing code, and now you know what it is.

All four mechanisms propagate NULL: if the column value is NULL, the match expression returns NULL, which WHERE treats as false.

Why is LIKE with a leading wildcard slow?

The one thing that trips people up: a leading % prevents index use.

LIKE 'prefix%' — a pattern anchored at the start — can use a B-tree index on the column. LIKE '%suffix' or LIKE '%anywhere%' cannot. PostgreSQL has to scan every row. The same applies to ILIKE and the regex operators. On large tables, where wildcards appear in your pattern directly affects query speed.

Check your understanding

You want to find all emails containing 'gmail' anywhere, case-insensitive. Which is correct?

Practice Pattern Matching in SQL

Practice · easy hr · Helix Systems

Helix Systems' CRM team is pulling a list of customer-facing roles — anyone whose title mentions an Account function.

Write a query to return the ID, name, and title of every employee whose title contains the word Account, regardless of capitalization.

Assumptions:

  • The employees table has one row per employee with an id, a name, and a title.
  • A qualifying employee has a title that contains account somewhere in the string, with case ignored.

Output:

  • One row per qualifying employee, with columns id, name, and title.
Schema · hr4 tables? = nullable
departments
idinteger
nametext
locationtext
budgetnumeric
salaries
idinteger
employee_id?integer
amountnumeric
effective_datedate
end_date?date
employees
idinteger
nametext
emailtext
department_id?integer
manager_id?integer
hire_datedate
titletext
is_activeboolean
job_history
idinteger
employee_id?integer
titletext
department_id?integer
start_datedate
end_date?date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 Pattern Matching practice problems

Start learning to practice all 9 Pattern Matching problems, with instant grading and mastery tracking.

Common questions about Pattern Matching

Are the regex operators case sensitive?

The plain one is, and there is a second form that is not. Matching upper case text against a lower case pattern fails with the case-sensitive operator and succeeds with the case-insensitive one, which is the same split as LIKE and ILIKE.

Does a regex match the whole string or any part of it?

Any part, which is the opposite of LIKE. A pattern with no anchors finds its text anywhere in the value, so add a caret at the start or a dollar at the end when you mean to pin it to one end, or both to require an exact match.

Can you use regex syntax inside a LIKE pattern?

No. LIKE understands only its own two wildcards, so a character class written in a LIKE pattern is matched literally and quietly finds nothing. Switch to one of the regex operators when the pattern needs more than any-sequence and any-single-character.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.