Sub topic 4
Case Sensitivity and Portability
This chapter assumes that you know basic SELECT statements, tables, columns, comparison operators, and WHERE clauses. The examples use an employees table with an id and name column.
Why `LIKE` results differ
LIKE compares text against a pattern. The % wildcard matches any sequence of characters, and _ matches one character. Whether letters must have the same case depends on the database system, its collation, and sometimes the column definition.
A collation is a set of rules for comparing and sorting text. It can define whether comparisons ignore case, how accents are treated, and how characters are ordered.
The same query can therefore return different rows on different databases:
SELECT id, name
FROM employees
WHERE name LIKE 'a%';For example, a database using a case-sensitive comparison may return alice but not Alice. A database using a case-insensitive collation may return both.
| Environment or setting | Typical behavior of `LIKE` |
|---|---|
| Case-sensitive collation | `a%` does not match `Alice` |
| Case-insensitive collation | `a%` can match both `alice` and `Alice` |
| PostgreSQL `LIKE` | Usually case-sensitive; `ILIKE` provides case-insensitive matching |
| MySQL or SQL Server | Behavior commonly follows the column or database collation |
These are common behaviors, not universal defaults. Check the documentation and configuration for the database you use.
Writing a portable case-insensitive match
If your application requires the same case behavior across databases, normalize both sides of the comparison. LOWER converts text to lowercase before LIKE compares it.
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'a%';This treats Alice, ALICE, and alice as equivalent for this comparison. Applying LOWER to both the column and the pattern is important. Lowercasing only the pattern does not change the case of the stored value.
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE LOWER('A%');For simple English text, this approach is often predictable. It is not a complete definition of case-insensitive matching for every language. Uppercase and lowercase rules can vary for Unicode characters, and different databases can implement those rules differently.
A database-specific operator can be clearer when portability is not required. PostgreSQL, for example, supports ILIKE:
SELECT id, name
FROM employees
WHERE name ILIKE 'a%';ILIKE is convenient, but it is not standard SQL and may require changes when you move the query to another database.
Case sensitivity and indexes
An index is a data structure that helps a database find rows without examining every row. A normal index on name may help with a prefix search such as name LIKE 'A%', especially when the comparison uses the same collation as the index.
Wrapping the column in LOWER can prevent an ordinary index from being used:
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'a%';Some databases support an expression index, also called a functional index, on LOWER(name). That index must be created using database-specific syntax. Another option is to store a normalized search column, such as a lowercase name, and index that column.
A leading wildcard usually prevents efficient use of a normal ordered index:
SELECT id, name
FROM employees
WHERE name LIKE '%ann%';The database must generally search for ann anywhere in the value. Full-text search or a specialized text index may be a better choice for that requirement.
Exercise
Why can WHERE LOWER(name) LIKE 'a%' be more portable but slower than WHERE name LIKE 'A%'?
Show answer
LOWER makes the comparison less dependent on the database's default case rules, but applying a function to the column can prevent an ordinary index on name from being used.