TExcelTAD
Home › PostgreSQL › PostgreSQL LIKE & ILIKE

PostgreSQL LIKE & ILIKE

= only finds exact matches. To search inside text, such as all titles that start with "The" or emails at a certain domain, use pattern matching with LIKE.

Wildcards

Wildcard Matches Example Matches
% any sequence of characters, including none 'The %' "The Salt Road", "The Map Thief"
_ exactly one character '_ip' "tip", "zip", not "trip"
pgsql · try it yourself
SELECT title FROM books WHERE title LIKE 'The %';
       title
-------------------
 The Salt Road
 The Last Orbit
 The Quiet Witness
 The Age of Canals
 The Map Thief
(5 rows)

% can appear anywhere. Titles that contain "of" as a word:

pgsql · try it yourself
SELECT title FROM books WHERE title LIKE '% of %';
         title
------------------------
 Empires of Sand
 The Age of Canals
 A Short History of Tea
(3 rows)

The underscore matches one character exactly. Four-letter first names that end with "a":

pgsql · try it yourself
SELECT first_name FROM customers WHERE first_name LIKE '___a';
 first_name
------------
 Anna
 Emma
 Gina
 Isla
(4 rows)

Case-insensitive matching with ILIKE

LIKE is case-sensitive. PostgreSQL's ILIKE ignores case, which is usually what a search box needs:

pgsql · try it yourself
SELECT title FROM books WHERE title LIKE '%sql%';
SELECT title FROM books WHERE title ILIKE '%sql%';
 title
-------
(0 rows)

           title
---------------------------
 Learning SQL the Calm Way
(1 row)
Operator Same as
LIKE ~~
ILIKE ~~*
NOT LIKE !~~
NOT ILIKE !~~*

NOT LIKE

Customers whose email is not an example.com address. Note that NULL emails are not returned either: NULL NOT LIKE '…' is NULL, not true.

pgsql · try it yourself
UPDATE customers SET email = 'ben@carter.dev' WHERE id = 2;

SELECT first_name, email
FROM customers
WHERE email NOT LIKE '%@example.com';
UPDATE 1
 first_name |     email
------------+----------------
 Ben        | ben@carter.dev
(1 row)

Searching for a literal % or _

To match the wildcard characters themselves, escape them with a backslash (the default escape character), or choose another one with ESCAPE:

pgsql · try it yourself
CREATE TABLE promos (code text);
INSERT INTO promos VALUES ('SAVE10%'), ('SAVE10'), ('BOOK_DAY'), ('BOOKDAY');

SELECT code FROM promos WHERE code LIKE '%\%';
SELECT code FROM promos WHERE code LIKE 'BOOK!_%' ESCAPE '!';
CREATE TABLE
INSERT 0 4
  code
---------
 SAVE10%
(1 row)

   code
----------
 BOOK_DAY
(1 row)

Regular expressions

When LIKE is not expressive enough, PostgreSQL supports POSIX regular expressions with ~ (case-sensitive) and ~* (case-insensitive). Titles that end in a word of exactly four letters:

pgsql · try it yourself
SELECT title FROM books WHERE title ~ ' [A-Za-z]{4}
  

;
       title
--------------------
 The Salt Road
 Murder at Low Tide
 Empires of Sand
(3 rows)

Tip: A pattern that starts with % (like '%sql%') cannot use an ordinary B-tree index, so on large tables it scans every row. For fast text search, see Full-Text Search.

Test yourself

Which pattern matches every title that ends with "Road"?

  1. 'Road%'
  2. '%Road'
  3. '_Road'
  4. '%Road%'

What is the difference between LIKE and ILIKE?

  1. ILIKE is faster
  2. ILIKE ignores upper/lower case
  3. ILIKE supports regular expressions
  4. There is none

How many characters does _ match in a LIKE pattern?

  1. Zero or more
  2. Exactly one
  3. One or more
  4. Zero or one