Open In App

PostgreSQL – ILIKE operator

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The PostgreSQL ILIKE operator is used to query data based on pattern-matching techniques. Its result includes strings that are case-insensitive and follow the mentioned pattern.

It is important to know that PostgreSQL provides 2 special wildcard characters for the purpose of patterns matching as below:

  • Percent ( %) for matching any sequence of characters.
  • Underscore ( _) for matching any single character.

Syntax of ILIKE Operator

coloumn_name ILIKE pattern;

For the sake of this article we will be using the sample DVD rental database, which is explained here. Now, let’s look into a few examples.

Examples of PostgreSQL ILIKE Operator

Example 1:

Here we will make a query to find the customer in the “customer” table by looking at the “first_name” column to see if there is any value that begins with “ke” using the ILIKE operator in our sample database.

Code:

SELECT
    first_name,
        last_name
FROM
    customer
WHERE
    first_name ILIKE 'Ke%';

Output:

output

 

Notice few things in the above example, the WHERE clause contains a special expression: the first_name, the LIKE operator, and a string that contains a percent (%) character, which is referred to as a pattern.

Example 2:

Here we will query for customers whose first name begins with any single character, is followed by the literal string “aR”, and ends with any number of characters using the ILIKE operator in our sample database.

Code:

SELECT
    first_name,
    last_name
FROM
    customer
WHERE
    first_name ILIKE '_aR%';

Output:

output

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads