Sub topic 4
Case sensitivity and portability
Whether LIKE 'a%' matches Alice depends on the database, the column's collation, and sometimes a setting. This chapter explains why the same query can return different rows on different systems. It also shows how to write a case-insensitive match that behaves the same everywhere, and what that costs.
It assumes you can write a basic SELECT ... WHERE, you know the % and _ wildcards from the previous chapters, and you know what an index is at a high level.
The standard says little, so databases differ
The SQL standard defines LIKE as a pattern test. It leaves the question "is A equal to a?" to the collation. A collation is the set of rules a database uses to compare and sort text. It decides whether A and a are equal and whether e and é are equal. A collation is attached to a column, a database, or a server default.
Because defaults differ, one query gives different answers depending on where it runs.
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO employees (id, name) VALUES
(1, 'Alice'),
(2, 'alan'),
(3, 'Bob'),
(4, 'ANDREA');
SELECT id, name
FROM employees
WHERE name LIKE 'A%';The last query returns different rows depending on the system:
| Database (default setup) | Rows returned | Why |
|---|---|---|
| PostgreSQL | Alice, ANDREA | `LIKE` is case-sensitive |
| Oracle | Alice, ANDREA | Comparisons are case-sensitive by default |
| MySQL, MariaDB | Alice, alan, ANDREA | Default collations end in `_ci` (case-insensitive) |
| SQL Server | Alice, alan, ANDREA | Default collations contain `CI` |
| SQLite | Alice, alan, ANDREA | `LIKE` ignores case for ASCII letters |
Treat this table as defaults, not guarantees. Anyone can create a database or column with a different collation, and then the row in the table no longer applies.
How each database decides
PostgreSQL
LIKE is always case-sensitive. PostgreSQL adds a non-standard operator, ILIKE, which ignores case.
SELECT id, name
FROM employees
WHERE name ILIKE 'a%';ILIKE is convenient, but no other major database accepts it. A query that uses it will not run on MySQL, SQL Server, or Oracle. PostgreSQL also offers the citext extension, a column type that compares case-insensitively, so plain LIKE and = on it ignore case.
MySQL and MariaDB
The result depends on the collation. In MySQL 8.0 the default collation is utf8mb4_0900_ai_ci. The ai means accent-insensitive and the ci means case-insensitive, so LIKE 'a%' matches Alice. To force a case-sensitive comparison for one query, name a binary or case-sensitive collation:
SELECT id, name
FROM employees
WHERE name COLLATE utf8mb4_bin LIKE 'A%';Older code often uses LIKE BINARY 'A%'. The BINARY operator is deprecated in MySQL 8.0, so prefer COLLATE.
SQL Server
Case sensitivity comes from the collation, and the common default, SQL_Latin1_General_CP1_CI_AS, is case-insensitive. Collation names containing CS are case-sensitive. You can override the collation per expression:
SELECT id, name
FROM employees
WHERE name COLLATE Latin1_General_CS_AS LIKE 'A%';SQLite
LIKE ignores case, but only for ASCII letters. LIKE 'é%' will not match É unless you load an extension. SQLite also has GLOB, a case-sensitive operator that uses Unix-style * and ? wildcards instead of % and _.
Oracle
Comparisons are case-sensitive unless the session sets NLS_COMP and NLS_SORT to linguistic, case-insensitive values. Most code does not rely on that. Use UPPER() or LOWER(), or REGEXP_LIKE with the 'i' flag.
The portable approach: normalize both sides
The only technique that works on every database is to change the case of the column yourself, so the result no longer depends on the collation:
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'a%';On a case-sensitive database this returns Alice, alan, and ANDREA. On a case-insensitive database it returns the same three rows, because LOWER(name) and the pattern are both already lowercase.
The pattern must be lowercase too
A common mistake is to lowercase the column and forget the pattern:
-- Returns no rows on any database
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'A%';After LOWER(), no value contains an uppercase A, so a case-sensitive database matches nothing. A case-insensitive database may still match, which hides the bug until you move the query. When the pattern comes from user input, lowercase it in application code, or apply LOWER() to the parameter in SQL:
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE LOWER('A%');`UPPER()` or `LOWER()`?
Either works. Pick one and use it throughout your codebase, so that function-based indexes (see below) can be shared across queries. UPPER() has one small advantage in some languages. A few characters have no round-trip lowercase form, such as the German ß, which UPPER() turns into SS on some systems and leaves unchanged on others. Fully reliable matching of non-English text needs a proper Unicode collation, not a function call.
Edge cases to know about
- Accents. Case-insensitive does not mean accent-insensitive. In a
_ci_ascollation,eandéare different. In an_aicollation they are equal.LOWER()never removes accents. - Turkish and similar locales. In Turkish, the uppercase of
iisİ, notI. A function using the wrong locale can produce surprising matches. This is rare, but it is a real failure for software with Turkish-speaking users. - Wildcards are unaffected.
%and_behave the same regardless of case rules. TheESCAPEmechanism from the previous chapter is independent of collation. - Trailing spaces. Some collations and
CHARcolumns treat'abc'and'abc 'as equal in=comparisons, butLIKEis stricter. This is a separate trap from case, but it shows up in the same portability tests. NULLis not affected.LOWER(NULL)isNULL, so rows with aNULLname never match eitherLIKEorNOT LIKE.
The cost: indexes
Wrapping a column in a function usually stops the database from using an ordinary index on that column. The index stores the original values, such as Alice. It does not store alice, so the database cannot use it to find rows where LOWER(name) starts with a. On a large table the query falls back to a full scan.
The fix is an index on the expression itself. The syntax varies by database:
| Database | Option |
|---|---|
| PostgreSQL | `CREATE INDEX idx_emp_lower_name ON employees (LOWER(name));` |
| Oracle | Function-based index on `LOWER(name)` |
| MySQL 8.0.13 and later | Functional key part: `CREATE INDEX idx_emp_lower_name ON employees ((LOWER(name)));` |
| SQL Server | Computed column `LOWER(name)`, then index the computed column |
| SQLite | `CREATE INDEX idx_emp_lower_name ON employees (LOWER(name));` |
The query must use exactly the same expression as the index, or the database will not match them. Also, an index only helps a pattern with a fixed start. LIKE 'a%' can use it, while LIKE '%a' cannot, as the index chapter explains.
On a case-insensitive collation, a plain index on name already supports LIKE 'a%' in most cases. That is the reason many teams choose a case-insensitive collation, or citext, for columns that are normally searched by text.
Choosing an approach
- You control the schema and only target one database. Use that database's native mechanism: a case-insensitive collation,
citext, orILIKE. - The query must run on several databases. Use
LOWER(column) LIKE LOWER(pattern), and add an expression index where the platform allows it. - The column is large and searched often. Create the expression index first, and check the query plan to confirm it is used.
- The text is multilingual. Do not rely on
LOWER(). Configure a Unicode-aware collation for the column.
Exercise
A query SELECT name FROM employees WHERE LOWER(name) LIKE 'Al%'; returns no rows on PostgreSQL, although the table contains Alice and alan. Why, and how do you fix it so it also behaves the same on MySQL?
Show answer
LOWER(name) produces lowercase text such as alice, but the pattern 'Al%' starts with an uppercase A. On PostgreSQL, where LIKE is case-sensitive, no value matches. MySQL's case-insensitive default collation would hide the bug and return rows. The fix is to lowercase the pattern as well: WHERE LOWER(name) LIKE 'al%', or WHERE LOWER(name) LIKE LOWER('Al%') when the pattern is supplied at run time.
Exercise
Your team runs WHERE LOWER(email) LIKE 'sam%' on a table with millions of rows, and the query is slow even though email has an ordinary index. What is the cause, and what do you change?
Show answer
The ordinary index stores the original email values, so the database cannot use it for a condition on LOWER(email). It scans the whole table. Create an index on the expression, for example CREATE INDEX idx_users_lower_email ON users (LOWER(email)); in PostgreSQL, and keep the query's expression identical to the index's. On a database with a case-insensitive collation, you could instead drop LOWER() and rely on the collation and the plain index.