← Back to SQL, page 1

sql-0011 · Medium mid

Is SQL case-sensitive? Consider keywords, identifiers and the data itself.

Keywords are never case-sensitive. Unquoted identifiers are case-insensitive in most engines, and PostgreSQL folds them to lower case. Data comparisons depend on the engine and collation: SQLite and PostgreSQL compare strings with = case-sensitively, SQLite's LIKE ignores ASCII case, PostgreSQL's LIKE does not, and MySQL's default collations ignore case.

Full answer

The question has three parts, and the answer differs for each.

Keywords such as select and SELECT mean the same thing everywhere.

Unquoted identifiers are case-insensitive. PostgreSQL does this by folding them to lower case, so Employees and employees are the same table. Quoting an identifier keeps its exact case: "Employees" in PostgreSQL is a different name from employees.

The data is where engines differ. On SQLite, = is case-sensitive but LIKE ignores case for ASCII letters:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT
  (SELECT COUNT(*) FROM employees WHERE name = 'asha')    AS eq_lower,
  (SELECT COUNT(*) FROM employees WHERE name LIKE 'asha')  AS like_lower;
eq_lowerlike_lower
01

PostgreSQL treats both as case-sensitive, and offers ILIKE for a case-insensitive match:

sql
SELECT
  (SELECT COUNT(*) FROM employees WHERE name = 'asha')     AS eq_lower,
  (SELECT COUNT(*) FROM employees WHERE name LIKE 'asha')  AS like_lower,
  (SELECT COUNT(*) FROM employees WHERE name ILIKE 'asha') AS ilike_lower;
eq_lowerlike_lowerilike_lower
001

For portable case-insensitive matching, compare lower-cased values: LOWER(name) = LOWER('asha'). Be aware this can stop a plain index on name from being used. PostgreSQL can index the expression LOWER(name), and SQLite can declare a column COLLATE NOCASE.

MySQL's default collations are case-insensitive, so 'asha' = 'Asha' is true there. That comes from the MySQL documentation and was not run here.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT
  (SELECT COUNT(*) FROM employees WHERE name = 'asha')    AS eq_lower,
  (SELECT COUNT(*) FROM employees WHERE name LIKE 'asha')  AS like_lower;

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

Key points

  • Keywords: never case-sensitive
  • Unquoted identifiers: case-insensitive; PostgreSQL folds them to lower case
  • Data: depends on engine and collation
  • SQLite LIKE ignores ASCII case; PostgreSQL LIKE does not; ILIKE does

Common mistakes

  • Creating a PostgreSQL table with a quoted mixed-case name and then querying it unquoted
  • Wrapping an indexed column in LOWER() and losing the index

Follow-ups

  • How would you make a case-insensitive unique constraint on email?
  • What is a collation?

Tips

  • Split the answer into keywords, identifiers and data; it shows structured thinking