← Back to SQL, page 8

sql-0141 · Medium mid

Is LIKE case-sensitive? Compare SQLite and PostgreSQL for WHERE email LIKE 'ben%'.

SQLite's LIKE is case-insensitive for ASCII letters by default, so 'ben%' matches 'Ben@...'. PostgreSQL's LIKE is case-sensitive and returns nothing; it offers ILIKE for case-insensitive matching. A portable form is LOWER(email) LIKE 'ben%'.

Full answer
sql
CREATE TABLE contacts (
  id    INTEGER PRIMARY KEY,
  first TEXT,
  last  TEXT,
  email TEXT,
  phone TEXT
);
INSERT INTO contacts VALUES
  (1,'Asha','Rao','asha.rao@example.com','+91 98450-12345'),
  (2,'ben',NULL,'Ben@Mail.example.org','(020) 7946 0018'),
  (3,'  Chen ','Li','chen_li@example.com','020-7946-0019'),
  (4,'Dara','Okafor',NULL,'12345');
sql
SELECT id, email FROM contacts WHERE email LIKE 'ben%' ORDER BY id;
idemail
2Ben@Mail.example.org
sql
SELECT id, email FROM contacts WHERE email LIKE 'ben%' ORDER BY id;
idemail

(0 rows)

The same query finds Ben on SQLite and nothing on PostgreSQL. PostgreSQL's ILIKE matches:

sql
SELECT id, email FROM contacts WHERE email ILIKE 'ben%' ORDER BY id;
idemail
2Ben@Mail.example.org

The portable version lowercases the column, which works on both engines:

sql
SELECT id, email FROM contacts WHERE LOWER(email) LIKE 'ben%' ORDER BY id;
idemail
2Ben@Mail.example.org

LOWER(email) on the column prevents a plain index on email from being used. PostgreSQL can index the expression lower(email), or use the citext type (not run here). SQLite's case-folding in LIKE covers ASCII only; it does not fold letters such as Ä without an extension.

sql
CREATE TABLE contacts (
  id    INTEGER PRIMARY KEY,
  first TEXT,
  last  TEXT,
  email TEXT,
  phone TEXT
);
INSERT INTO contacts VALUES
  (1,'Asha','Rao','asha.rao@example.com','+91 98450-12345'),
  (2,'ben',NULL,'Ben@Mail.example.org','(020) 7946 0018'),
  (3,'  Chen ','Li','chen_li@example.com','020-7946-0019'),
  (4,'Dara','Okafor',NULL,'12345');

SELECT id, email FROM contacts WHERE email LIKE 'ben%' ORDER BY id;

SELECT id, email FROM contacts WHERE LOWER(email) LIKE 'ben%' ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • SQLite LIKE ignores ASCII case by default
  • PostgreSQL LIKE is case-sensitive; ILIKE is not
  • LOWER(col) LIKE lower-pattern is portable
  • Functions on the column need an expression index

Common mistakes

  • Testing a search on SQLite and shipping it to PostgreSQL
  • Lowercasing the column but not the pattern

Follow-ups

  • How would you index a case-insensitive email lookup?
  • Is = case-sensitive in SQLite?

Tips

  • Mention both the behaviour and the index cost