Cheatsheet Pages / SQL Cheatsheet

How to match text patterns in SQL — LIKE % and _

You need names starting with a given letter, or any text pattern, without loading the whole table.

Every example runs against the cheatsheet's sample tables — departments(id, name), employees(id, name, dept_id, salary, manager_id, hired_on) and orders(id, emp_id, amount, ordered_on) — and was executed and verified against SQLite, so you can paste it in and get the same result.

SELECT name FROM employees WHERE name LIKE 'A%';
-- Alice. % matches any run of characters, _ matches exactly one
-- Dialect: LIKE is case-insensitive for ASCII in SQLite / MySQL, but case-sensitive in PostgreSQL.
--          PostgreSQL also has ILIKE for case-insensitive matching (PG only; MySQL / SQLite don't have it)
SQL Cheatsheet one-page card — queries that actually work
The full one-page card — SQL Cheatsheet. Get the complete cheatsheet on GitHub