PostgreSQL LIKE, Not Like, Wildcards (%, _ ) Examples

โšก Smart Summary

PostgreSQL LIKE matches text against patterns using two wildcards โ€” the percent sign and the underscore. This tutorial shows how to use LIKE and NOT LIKE in SELECT statements, with worked psql and pgAdmin examples for each case.

  • ๐Ÿ” Core Operator: LIKE compares a column or expression against a wildcard pattern and returns true on a match.
  • ๐ŸŒŸ Two Wildcards: % matches zero or more characters; _ matches exactly one character.
  • โœ‹ Inverse Match: Combine NOT with LIKE to return rows that do not match the pattern.
  • ๐Ÿ› ๏ธ Two Interfaces: Run the same queries from the psql command line or graphically inside pgAdmin.
  • ๐ŸŽฏ Escape Character: Use the ESCAPE clause to match literal % or _ characters inside the pattern.
  • ๐Ÿค– AI Boost: AI database tools translate plain-language search needs into LIKE patterns and suggest ILIKE when case-insensitive matching is required.

PostgreSQL LIKE, Not Like, Wildcards (%, _ ) Examples

The PostgreSQL LIKE operator matches text values against patterns using wildcards. If the search expression matches the pattern, LIKE returns true. Wildcards work in the WHERE clause of SELECT, UPDATE, INSERT, or DELETE.

What are PostgreSQL Wildcards?

PostgreSQL supports two LIKE wildcards:

  • Percent sign (%): matches zero, one, or many characters or digits.
  • Underscore (_): matches exactly one character or digit.

The two symbols can be combined inside the same pattern. If LIKE is used without either wildcard, it behaves like the equals (=) operator.

PostgreSQL LIKE Syntax

The basic LIKE syntax is:

expression LIKE pattern [ ESCAPE 'escape-character' ]
  • expression โ€” a character expression, typically a column or field name.
  • pattern โ€” a character expression that contains wildcards.
  • escape-character โ€” optional. Lets you match literal % or _ characters. When omitted, the backslash (\) is the default escape character.

PostgreSQL LIKE with the % Wildcard

The % sign matches zero, one, or more characters. Consider the following Book table:

Sample Book table used in the LIKE examples

To find books whose name starts with “Lear”, run the query below.

SELECT *
FROM
   Book
WHERE
   name LIKE 'Lear%';

The query returns:

LIKE Lear% query result returning matching books

To find books whose name contains “by” anywhere:

SELECT *
FROM
   Book
WHERE
   name LIKE '%by%';

The query returns:

LIKE %by% query result with matching books

PostgreSQL LIKE with the _ Wildcard

The _ sign matches exactly one character. The following query finds names where the first character is any single letter, followed by “earn” and then any suffix:

SELECT *
FROM
   Book
WHERE
   name LIKE '_earn%';

The query returns:

LIKE _earn% query result with single-character prefix matches

Another example โ€” match any text that ends with “Beginner” plus one more character:

SELECT *
FROM
   Book
WHERE
   name LIKE '%Beginner_';

The query returns:

LIKE %Beginner_ query result matching trailing single character

PostgreSQL NOT LIKE Operator

Combine LIKE with NOT to return rows that do not match the pattern. For example, list every book whose name does not start with “Post”:

SELECT *
FROM
   Book
WHERE
   name NOT LIKE 'Post%';

The query returns:

NOT LIKE Post% result excluding books that start with Post

Only one book satisfies the condition. Now list every book whose name does not contain the word “Made”:

SELECT *
FROM
   Book
WHERE
   name NOT LIKE '%Made%';

The query returns:

NOT LIKE %Made% result returning three matching books

Three rows satisfy the condition.

Using LIKE with pgAdmin

The same queries can also be run graphically inside pgAdmin’s Query Tool.

% Wildcard in pgAdmin

Step 1) Log in to pgAdmin.

Step 2) In the navigation bar on the left, click Databases, then click Demo.

pgAdmin navigation tree with Databases and Demo expanded

Step 3) Type the query in the Query Editor:

SELECT *
FROM
   Book
WHERE
   name LIKE 'Lear%';

Step 4) Click Execute.

pgAdmin Query Tool with the Execute toolbar button

The result pane shows the matching books:

pgAdmin result pane for LIKE Lear% query

To search for a book with “by” anywhere in its name:

Step 1) Type the following in the Query Editor:

SELECT *
FROM
   Book
WHERE
   name LIKE '%by%';

Step 2) Click Execute.

pgAdmin Query Tool running LIKE %by% query

The result pane shows:

pgAdmin result pane for LIKE %by% query

_ Wildcard in pgAdmin

Step 1) Log in to pgAdmin.

Step 2) In the navigation bar on the left, click Databases, then click Demo.

pgAdmin navigation tree showing Databases tree

Step 3) Type the query in the Query Editor:

SELECT *
FROM
   Book
WHERE
   name LIKE '_earn%';

Step 4) Click Execute.

pgAdmin Query Tool running LIKE _earn% query

The result pane shows:

pgAdmin result pane for LIKE _earn% query

Step 5) Run the second example:

  1. Type the query in the Query Editor:
SELECT *
FROM
   Book
WHERE
   name LIKE '%Beginner_';
  1. Click Execute.

pgAdmin Query Tool running LIKE %Beginner_ query

The result pane shows:

pgAdmin result pane for LIKE %Beginner_ query

NOT LIKE in pgAdmin

Step 1) Log in to pgAdmin.

Step 2) In the navigation bar on the left, click Databases, then click Demo.

pgAdmin navigation tree for NOT LIKE example

Step 3) To list every book whose name does not start with “Post”, type:

SELECT *
FROM
   Book
WHERE
   name NOT LIKE 'Post%';

Step 4) Click Execute.

pgAdmin Query Tool running NOT LIKE Post% query

The result pane shows:

pgAdmin result pane for NOT LIKE Post% query

Additional pgAdmin result detail for NOT LIKE query

To list books whose name does not contain the word “Made”:

Step 1) Type the following in the Query Editor:

SELECT *
FROM
   Book
WHERE
   name NOT LIKE '%Made%';

Step 2) Click Execute.

pgAdmin Query Tool running NOT LIKE %Made% query

The result pane shows:

pgAdmin result pane for NOT LIKE %Made% query

Download the database used in this tutorial.

FAQs

LIKE is case-sensitive while ILIKE performs case-insensitive matching. ILIKE is a PostgreSQL extension. For example, name ILIKE ‘lear%’ matches “Learn” and “LEAR” alike. Use ILIKE when user input case is unpredictable.

Use the ESCAPE clause to declare an escape character, then prefix the literal wildcard. For example, WHERE code LIKE ’50!%%’ ESCAPE ‘!’ finds values starting with “50%” because the exclamation mark escapes the percent sign.

Use LIKE for simple prefix, suffix, or substring matches. Switch to POSIX regex (~ and ~*) when you need character classes, alternation, or quantifiers. Regex is more powerful but typically slower than LIKE for basic patterns.

Yes. AI assistants such as text-to-SQL copilots translate prompts like “find customers whose email contains acme” into name LIKE ‘%acme%’ and explain the wildcard logic, accelerating ad-hoc reporting.

Generative AI inspects the query plan, recommends a trigram index (pg_trgm) for leading-wildcard LIKE searches, and rewrites patterns that prevent index usage. This turns full table scans into millisecond lookups on large PostgreSQL tables.

Summarize this post with: