// REF. 12 / QUERIES
SQL cheat sheet
Portable SQL: the SELECT clauses in the order the engine runs them, every join, grouping, subqueries and CTEs, window functions, and the writes and transactions that keep your data honest. Syntax checked against the PostgreSQL 18 reference, written to work anywhere standard SQL does.
Two habits: prefer NOT EXISTS over NOT IN (one null and you get zero rows), and never trust row order without an ORDER BY.
↓ Download the printable PDF free · letter size · no email needed
Query
SELECT a, b FROM t- pick columns
WHERE x = 1 AND y > 2- filter rows before grouping
ORDER BY x DESC, y- sort; ASC is the default
LIMIT 10 OFFSET 20- page three, ten rows a page
SELECT DISTINCT a- drop duplicate rows
a BETWEEN 1 AND 10- inclusive on both ends
x LIKE 'a%'- % any run, _ one character
x IN (1, 2, 3)- matches any listed value
Joins
JOIN b ON a.id = b.a_id- inner: rows that match on both sides
LEFT JOIN b ON ...- all of a; b columns null if no match
RIGHT JOIN- all of b; the mirror of LEFT
FULL JOIN- all of both, nulls where unmatched
CROSS JOIN b- every pair; the cartesian product
Grouping
GROUP BY a- one row per distinct a
COUNT(*)- rows, nulls included
COUNT(col)- non-null values only
SUM, AVG, MIN, MAX- ignore nulls; null on empty input
HAVING COUNT(*) > 1- filter groups, after GROUP BY
COUNT(DISTINCT a)- distinct non-null values
Subqueries & CTES
EXISTS (SELECT 1 ...)- true if the subquery returns a row
x IN (SELECT ...)- same as = ANY of the subquery
x NOT IN (SELECT ...)- null-poisoned; prefer NOT EXISTS
WITH t AS (SELECT ...)- named subquery; chain with commas
WITH RECURSIVE t AS (..)- base UNION ALL a step that reads t
(SELECT ...) AS sub- a subquery in FROM needs an alias
Windows
f() OVER (...)- per-row aggregate, no collapsing
OVER (PARTITION BY a)- restart the window per group
ROW_NUMBER() OVER (..)- 1, 2, 3 in ORDER BY order
RANK() / DENSE_RANK()- ties share; with gaps / without
LAG(x) / LEAD(x)- previous / next row's value
SUM(x) OVER (ORDER BY d)- running total
Null & case
x IS NULL- the only way to test for null
COALESCE(a, b, 'none')- first non-null argument
NULLIF(a, b)- null if equal, else a
CASE WHEN c THEN 1 END- no ELSE and no match: null
x IS DISTINCT FROM y- <> that treats nulls as values
Writes & txns
INSERT INTO t (a, b)- then VALUES (1, 2), (3, 4)
UPDATE t SET a = 1 WHERE- no WHERE: every row
DELETE FROM t WHERE ...- no WHERE: every row, no undo
BEGIN; ... COMMIT;- all or nothing
ROLLBACK- undo everything since BEGIN
SAVEPOINT s- a partial undo point
ROLLBACK TO SAVEPOINT s- back to s, transaction stays open
Schema
CREATE TABLE t (...)- columns, types, constraints
id INT PRIMARY KEY- unique and not null; one per table
email TEXT UNIQUE- no duplicates; nulls may repeat
x INT NOT NULL- required
CHECK (price > 0)- row rule; a null passes
REFERENCES t (id)- foreign key; add ON DELETE CASCADE
CREATE INDEX i ON t (a)- speeds WHERE, JOIN, ORDER BY on a
Here be dragons
x = NULL- always null; use IS NULL
NOT IN (... NULL ...)- one null, zero rows back
LEFT JOIN + WHERE b.x- quietly becomes an inner join
no ORDER BY- row order is never promised
// KEEP IT UNDER YOUR KEYBOARD
SQL Field Manual Desk Mat
from $34 · printed to order in the USA