PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - ILIKE Clause



The PostgreSQL ILIKE clause is used in a WHERE clause to search for a specified pattern in a specified column. The wildcards which are used in conjunction with the ILIKE clause are given below:

Note: The NOT ILIKE clause is the negation of ILIKE clause.

Note: The ILIKE clause is similar to LIKE clause except the ILIKE clause performs case-insensitive match according to the active locale and LIKE clause performs case-sensitive match according to the active locale.

Wildcard Characters in PostgreSQL

SymbolDescriptionExample
%Represents zero, one or multiple characters.'J%' represents a value that start with "J", for example - John, Jo and Jack etc.
_Represents one character.'_o%' represents a value that have "o" in the second position, for example - John, Jo and Journey etc.
ESCAPEAllow test for literal instances of a wildcard character such as % or _'J%!%' ESCAPE '!' represents a value that starts with J and ends in %, for example - John%, Jo% and Journey% etc.

Note: The operator ~~* is equivalent to ILIKE. Similarly, !~~* operator represents NOT ILIKE.

Syntax

The syntax for using ILIKE Clause in PostgreSQL is given below:

SELECT column1, column2, ...
FROM table_name
WHERE column ILIKE pattern;

The table below describes patterns which is used with ILIKE clause and uses (%) and (_).

PatternDescription
'J%'A value that start with "J".
'%n'A value that end with "n".
'%oh%'A value that have "oh" in any position.
'_o%'A value that have "o" in the second position.
'J_%'A value that start with "J" and have at least 2 characters.
'J__%'A value that start with "J" and have at least 3 characters.
'J%n'A value that start with "J" and ends with "n".

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To fetch all records from Employee table where where the City starts with 'New', the query is:

    SELECT * FROM Employee
    WHERE City ILIKE 'New%';
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    5RameshNew Delhi283000
  • Similarly, to fetch all records from Employee table where Name of the employee starts with 'Jo', the query is:

    SELECT * FROM Employee
    WHERE Name ILIKE 'Jo%';
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    3JoParis272800
  • Using NOT ILIKE operator: NOT ILIKE operator is used as the negation of ILIKE operator. For example, to select all records of the Employee table with Name not starting with 'Jo', the following query can be used:

    SELECT * FROM Employee
    WHERE Name NOT ILIKE 'Jo%';
    

    This will produce the result as shown below:

    EmpIDNameCityAgeSalary
    2MarryNew York242750
    4KimAmsterdam303100
    5RameshNew Delhi283000
    6HuangBeijing282800