// dialect comparison

Case-insensitive string comparison

Whether = ignores case depends entirely on the column's collation.

Filed under Querying

The verdict

lower(email) = '[email protected]', backed by an index on the expression, is the only form that behaves the same on all five engines. Relying on defaults means the same query is case-insensitive on SQL Server and MySQL and case-sensitive on the other three. Watch SQLite's NOCASE and lower(): both fold ASCII only, so 'Ä' never matches 'ä'.

Where they differ

SQL Server and MySQL compare case-insensitively by default; PostgreSQL, SQLite and Oracle do not. The portable habit is lower(x) = lower(y), backed by an expression index so it stays fast.

How each engine writes it

SQL Server

SELECT * FROM users WHERE email = '[email protected]';

The default collations (SQL_Latin1_General_CP1_CI_AS and friends) are case-insensitive, so this matches any casing. COLLATE Latin1_General_CS_AS on the column or the comparison makes it case-sensitive.

PostgreSQL

SELECT * FROM users WHERE lower(email) = '[email protected]';

Case-sensitive by default. email ILIKE '[email protected]' also works; an index on lower(email), the citext type or a nondeterministic ICU collation (12+) keep it indexable.

MySQL

SELECT * FROM users WHERE email = '[email protected]';

Default *_ci collations match any casing, and utf8mb4_0900_ai_ci ignores accents too. A _bin or _cs collation makes the comparison case-sensitive.

SQLite

SELECT * FROM users WHERE email = '[email protected]' COLLATE NOCASE;

= on TEXT is case-sensitive by default. NOCASE folds ASCII letters only; declare the column (or an index) COLLATE NOCASE so the comparison can use an index.

Oracle

SELECT * FROM users WHERE LOWER(email) = '[email protected]';

Case-sensitive by default. NLS_COMP=LINGUISTIC with NLS_SORT=BINARY_CI makes a session case-insensitive; 12.2+ also allows a column-level COLLATE BINARY_CI.

Questions that come up

Why does my query match on one server but not another?

The collations differ. SQL Server and MySQL install with case-insensitive defaults; PostgreSQL, SQLite and Oracle compare case-sensitively. Collation can also differ per database and per column. Check sys.columns.collation_name in SQL Server, information_schema.columns.collation_name in MySQL and PostgreSQL, and NLS_COMP/NLS_SORT in Oracle.

How do I keep a lower() comparison indexed?

Index the exact expression the query uses: CREATE INDEX ix_users_email_lower ON users (LOWER(email)) works in PostgreSQL, Oracle and SQLite; MySQL 8.0.13+ needs the expression in double parentheses, ((LOWER(email))); SQL Server needs a computed column with an index on it. Without one the predicate scans the table.

What is citext?

A PostgreSQL extension type (CREATE EXTENSION citext) that stores the original text but compares it case-insensitively, so plain = and ordinary indexes just work. It changes equality for the whole column, including joins, DISTINCT and unique constraints.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.