← Back to PostgreSQL Course | Chapter 5: Filtering & Functions | Lesson 7 of 7

LIKE/ILIKE

LIKE matches text against patterns, and ILIKE does the same ignoring case.

In this page:

  1. LIKE/ILIKE
Syntax
sql
WHERE column LIKE 'pattern%'
WHERE column ILIKE '%pattern%'    -- case-insensitive

LIKE/ILIKE

In patterns % matches any number of characters and _ matches exactly one. LIKE is case-sensitive in PostgreSQL, ILIKE is case-insensitive, and ~ and ~* match regular expressions.

Leading wildcards such as '%abc' cannot use a normal B-tree index, though pg_trgm indexes can help. SQLite's LIKE is case-insensitive and has no ILIKE, so this is shown in psql.

Note: For fast substring search, use a pg_trgm GIN index.

Example: LIKE/ILIKE

bash
shop=# SELECT name FROM users WHERE name LIKE 'A%';
 name
------
 Ada
shop=# SELECT name FROM users WHERE name ILIKE 'a%';
 name
------
 Ada
 alan
shop=# SELECT name FROM users WHERE name LIKE '_o_';
 name
------
 Bob
shop=# SELECT name FROM users WHERE name ~* '^(ada|bob)$';

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Expecting LIKE to be case-insensitive in PostgreSQL
  2. Using LIKE for regular expressions
  3. Leading wildcards on large tables
Chapter Summary
  • % any characters, _ one character
  • LIKE is case-sensitive
  • ILIKE ignores case
  • ~ matches regular expressions
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.