Case Insensitive Search in SQL: LOWER, LIKE, COLLATE and Indexes
A case insensitive search finds “asha@example.com” when the user types “ASHA@Example.com”. It sounds trivial, but the method you choose decides whether the query uses an index or scans the whole table, and whether names like José and Straße match at all. We compared five approaches in SQLite with query plans and timings, and summarise how PostgreSQL, MySQL and SQL Server differ.
- A case insensitive search on
LOWER(email)scanned all 300,000 rows (43 ms) until we created an index on that exact expression; then it was an index search taking under 0.01 ms. - In SQLite,
LIKEandCOLLATE NOCASEignore case only for ASCII letters: “josé” did not match “JOSÉ”. - SQLite’s
lower('JOSÉ')returned “josÉ”; a custom function using Python’scasefold()matched both. - A
COLLATE NOCASE UNIQUEcolumn rejected a second email differing only in case.
Users do not care about capital letters. They type their email in any case, search for “iphone” when the product is “iPhone”, and expect a username to be unique whether it is “Sam” or “sam”. A case insensitive search in SQL handles that, but each database has different defaults and each technique has a different effect on performance and correctness.
We ran every test on SQLite 3.50.4 through Python. Notes on PostgreSQL, MySQL and SQL Server are based on their documentation and the research cited in this article; we did not run those systems.
Why case insensitive search is not trivial
- Defaults differ: MySQL and SQL Server usually compare text case-insensitively out of the box; PostgreSQL and SQLite compare case-sensitively.
- Functions block indexes: wrapping a column in
LOWER()hides it from a normal index. - Unicode is hard: some languages have letters with no simple one-to-one upper and lower case, such as German ß and Turkish dotless i.
Our test data
CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT, name TEXT);
-- Asha@Example.com Asha
-- asha@example.com asha dup
-- BEN@example.com Ben
-- José@example.com José
-- JOSÉ@EXAMPLE.COM JOSÉ
-- straße@example.com Strasse
CREATE TABLE big (id INTEGER PRIMARY KEY, email TEXT); -- 300,000 rows like User123@Example.com
CREATE INDEX ix_email ON big(email);
Method 1: plain equals is case-sensitive
SELECT name FROM users WHERE email = 'asha@example.com';
-- [('asha dup',)]
Only the exactly matching lowercase row came back; “Asha@Example.com” was missed. That is SQLite and PostgreSQL behaviour. In MySQL with its default collation, the same query would return both rows, which surprises developers moving between databases.
Method 2: LOWER on both sides
SELECT id FROM big WHERE lower(email) = 'user123@example.com';
-- plan: SCAN big 43.48 ms
Converting both sides to lowercase is the most portable approach and works in every database. But the query plan showed SCAN big: the index on email could not be used, because the index stores original values, not lowercase ones. On 300,000 rows that took 43 ms per query, and it grows linearly with the table. The fix is an index on the same expression, shown below.
Method 3: LIKE
SELECT name FROM users WHERE email LIKE 'asha@%';
-- [('Asha',), ('asha dup',)]
In SQLite, LIKE is case-insensitive for ASCII letters by default, so both rows matched. In PostgreSQL, LIKE is case-sensitive and ILIKE is the case-insensitive version. Turning on PRAGMA case_sensitive_like = ON in SQLite made LIKE 'user123@%' return 0 rows against our capitalised data. Patterns are explained in our SQL LIKE operator guide.
Method 4: COLLATE NOCASE
SELECT name FROM users WHERE email = 'ASHA@EXAMPLE.COM' COLLATE NOCASE;
-- [('Asha',), ('asha dup',)]
CREATE TABLE u2 (email TEXT COLLATE NOCASE UNIQUE);
A collation defines how text is compared and sorted. SQLite’s built-in NOCASE collation treats A to Z and a to z as equal. You can apply it in one query, as above, or declare it on the column so every comparison, sort and index on that column is case-insensitive automatically. Other databases have equivalents with different names, listed later.
The accent problem
SELECT name FROM users WHERE email LIKE 'josé@%';
-- [('José',)] JOSÉ@EXAMPLE.COM was not matched
SELECT lower('JOSÉ'), upper('straße');
-- ('josÉ', 'STRAßE')
This is the trap most tutorials skip. SQLite’s LIKE, NOCASE, lower() and upper() only fold the 26 ASCII letters. “É” stayed uppercase, so “josé” and “JOSÉ” did not match, and upper('straße') left the ß untouched. For an English-only system that may be acceptable. For names, cities and international product data, it silently misses records. SQLite’s optional ICU extension adds full Unicode folding; we did not have it in our build.
Method 5: a Unicode-aware function
db.create_function("pylower", 1, lambda s: s.casefold() if s else s, deterministic=True)
SELECT name FROM users WHERE pylower(email) = pylower('JOSÉ@EXAMPLE.COM');
-- [('José',), ('JOSÉ',)]
"straße".casefold() == "STRASSE".casefold() # True
From Python we registered a SQL function that calls str.casefold(), which implements full Unicode case folding, so both José rows matched. Casefold even maps ß to “ss”. Because we marked it deterministic, SQLite also allows an index on pylower(email), though that index then depends on the function being registered on every connection. In PostgreSQL, ICU-based collations play this role on the server.
Indexes and query plans
| Query on 300,000 rows | Plan | Time |
|---|---|---|
email = ? (exact case) |
SEARCH using ix_email | under 0.01 ms |
lower(email) = ? |
SCAN big | 43.48 ms |
email = ? COLLATE NOCASE |
SCAN big | 12.13 ms |
lower(email) = ? after CREATE INDEX ix_lower ON big(lower(email)) |
SEARCH using ix_lower | under 0.01 ms |
email = ? COLLATE NOCASE after CREATE INDEX ix_nocase ON big(email COLLATE NOCASE) |
SEARCH using ix_nocase | under 0.01 ms |
email LIKE 'user123@%' with ix_nocase present |
SEARCH using ix_nocase (range) | 0.01 ms |
The pattern is the same in every database: an index only helps if it was built with the same expression or collation the query uses. The ordinary ix_email index could serve neither lower(email) nor COLLATE NOCASE. An expression index on lower(email), or an index declared with the NOCASE collation, turned a full scan into a direct lookup thousands of times faster. SQLite even used the NOCASE index for a prefix LIKE, because its LIKE is case-insensitive by default. Note that a leading wildcard such as '%example.com' cannot use any of these indexes. For reading plans, see our SQL query optimization guide and indexes guide.
Tip: if you always search a column case-insensitively, store a normalised copy (for example email_lower) or declare the column with a case-insensitive collation, and index that. Then every query is simple and fast.
Case-insensitive uniqueness
INSERT INTO u2 VALUES ('Asha@Example.com');
INSERT INTO u2 VALUES ('asha@example.com');
-- IntegrityError: UNIQUE constraint failed: u2.email
Usernames and emails should usually be unique regardless of case, or two accounts can exist for one person. A unique constraint on a NOCASE column rejected the second insert. In PostgreSQL the equivalent is a unique index on lower(email) or a citext column. See our SQL constraints guide for unique constraints in general.
How other databases handle it
| Database | Default | Case-insensitive options |
|---|---|---|
| PostgreSQL | Case-sensitive | ILIKE, lower() with an expression index, the citext extension, nondeterministic ICU collations |
| MySQL | Case-insensitive (default _ci collations) |
Already insensitive; use a _bin or _cs collation for case-sensitive |
| SQL Server | Usually case-insensitive (CI collation) | COLLATE Latin1_General_CI_AS per query or column |
| SQLite | Case-sensitive for =, ASCII-insensitive for LIKE |
COLLATE NOCASE, expression index on lower(), ICU extension |
In PostgreSQL, ILIKE with a pattern cannot use a normal B-tree index; a trigram index from the pg_trgm extension handles it. Nondeterministic collations also have limits with LIKE in older versions. Check the documentation for your exact version before choosing.
Which method should you use?
- Portable and simple:
LOWER(col) = LOWER(?)plus an expression index onLOWER(col). - Always case-insensitive column: declare a case-insensitive collation (SQLite NOCASE, MySQL
_ci, PostgreSQL citext or ICU) and index it. - Partial matches:
LIKEorILIKEwith a prefix pattern, or full-text search for words inside text. - International text: use Unicode-aware folding (ICU collations or casefold in the application), and test with accented and non-Latin names.
- Normalise on write: lowercasing emails before storing them makes every later query trivial.
The same ideas appear in application code: Python’s regex module has an IGNORECASE flag, and string helpers are covered in our SQL string functions guide.
Final thoughts
A case insensitive search is easy to write and easy to get wrong. Choose one approach per column, make sure an index matches the expression or collation your queries use, and decide explicitly how accented letters should behave. In our tests, the difference between a scan and an index search was 43 milliseconds against a few microseconds, and the difference between ASCII-only and Unicode folding was a customer named José who could not be found.
Frequently asked questions
How do I make a SQL query case insensitive?
Compare LOWER of the column with LOWER of the value, use a case-insensitive collation, or in PostgreSQL use ILIKE. Add an index on the same expression or collation so the query stays fast.
Is SQL case sensitive?
Keywords are not, but data comparisons depend on the database and collation. MySQL and SQL Server usually ignore case by default, while PostgreSQL and SQLite compare case-sensitively.
Why is my LOWER query slow?
A normal index stores original values, so a query on LOWER of the column cannot use it and scans the table. Create an index on LOWER of the column.
Does COLLATE NOCASE work with accented letters?
Not in SQLite. NOCASE only folds the 26 ASCII letters, so an accented capital does not match its lowercase form. Use the ICU extension or a custom function for full Unicode folding.
What is ILIKE?
ILIKE is the case-insensitive version of LIKE in PostgreSQL. It is not standard SQL, so other databases use LOWER with LIKE or a case-insensitive collation instead.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.