SQL Cheat Sheet

SQL Cheat Sheet

Updated August 21, 2026

SQL cheat sheet online. Joins, data types, window functions, dates, and NULL handling for PostgreSQL, MySQL, and SQLite. Free searchable SQL reference.

SELECT

Basic

Core read query

SELECT col FROM t WHERE cond;
Distinct

Drop duplicate values

SELECT DISTINCT col FROM t;
Alias

AS is optional in most engines

SELECT u.name AS user_name FROM users u;
Limit

Postgres, MySQL, SQLite

SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20;
SQL Server page

T-SQL paging

SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

JOINs

INNER

Matches only

FROM a INNER JOIN b ON a.id = b.a_id
LEFT

Keep all of a

FROM a LEFT JOIN b ON a.id = b.a_id
RIGHT

Keep all of b

FROM a RIGHT JOIN b ON a.id = b.a_id
FULL

Keep leftovers on both sides

FROM a FULL OUTER JOIN b ON a.id = b.a_id
CROSS

Cartesian product

FROM a CROSS JOIN b
Anti

Rows in a with no b

FROM a LEFT JOIN b ON a.id = b.a_id WHERE b.id IS NULL
EXISTS

Semi-join, one row per a

WHERE EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)

NULL

IS NULL

Never use = NULL

WHERE col IS NULL
COALESCE

First non-null. All engines

COALESCE(a, b, c)
NULLIF

NULL if a equals b

NULLIF(a, b)
IFNULL

MySQL / SQLite two-arg

IFNULL(a, b)
ISNULL

SQL Server two-arg

ISNULL(a, b)

Data types

Postgres

Prefer TIMESTAMPTZ

INTEGER, BIGINT, NUMERIC, TEXT, BOOLEAN, TIMESTAMPTZ, JSONB, UUID
MySQL

BOOLEAN is TINYINT

INT, BIGINT, DECIMAL, VARCHAR(n), TINYINT(1), DATETIME, JSON
SQLite

Types are affinities

INTEGER, REAL, TEXT, BLOB, NUMERIC
SQL Server

NVARCHAR for unicode

INT, BIGINT, DECIMAL, NVARCHAR(n), BIT, DATETIME2, UNIQUEIDENTIFIER

Aggregates

Count

COUNT(*) counts null rows

COUNT(*) / COUNT(col) / COUNT(DISTINCT col)
Group

Non-aggregated cols must be grouped

SELECT u.id, COUNT(*) FROM t GROUP BY u.id
Having

Filter groups, not rows

GROUP BY u.id HAVING COUNT(*) > 1
Filter

Postgres / SQLite 3.30+

COUNT(*) FILTER (WHERE active)

Window functions

Row number

1..n per partition

ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC)
Rank

Ties skip the next rank

RANK() OVER (ORDER BY score DESC)
Lag / lead

Previous / next row

LAG(col, 1) OVER (ORDER BY ts)
Running sum

Frame defaults to RANGE UNBOUNDED PRECEDING

SUM(amount) OVER (PARTITION BY user_id ORDER BY ts)

Dates

Postgres trunc

Also week, month, year

DATE_TRUNC('day', ts)
MySQL date

MySQL date helpers

DATE(ts) / DATE_FORMAT(ts, '%Y-%m-%d')
SQLite

SQLite date as string

strftime('%Y-%m-%d', ts)
Interval

Postgres. MySQL: DATE_ADD(ts, INTERVAL 7 DAY)

ts + INTERVAL '7 days'
Now

NOW is Postgres/MySQL, GETDATE is T-SQL

CURRENT_TIMESTAMP / NOW() / GETDATE()

Strings

Concat

MySQL || is OR unless PIPES_AS_CONCAT

col || 'x'   -- Postgres, SQLite
CONCAT(col, 'x') -- MySQL, SQL Server
LIKE

% any, _ one char

WHERE name LIKE 'A%' ESCAPE '\'
ILIKE

Postgres only

WHERE name ILIKE '%smith%'
Trim

All engines

TRIM(BOTH FROM col) / LTRIM / RTRIM
Length

LEN is SQL Server

CHAR_LENGTH(col) / LEN(col)

INSERT / UPDATE / DELETE

Insert

List columns explicitly

INSERT INTO t (a, b) VALUES (1, 'x');
Insert select

Copy rows

INSERT INTO t (a) SELECT a FROM src;
Returning

Postgres, SQLite 3.35+

INSERT INTO t (name) VALUES ('Ada') RETURNING id;
Update

Always include WHERE

UPDATE t SET a = 1 WHERE id = 9;
Delete

DELETE FROM t; wipes the table

DELETE FROM t WHERE id = 9;
Upsert PG

PostgreSQL

INSERT INTO t (id, n) VALUES (1, 2)
ON CONFLICT (id) DO UPDATE SET n = EXCLUDED.n;
Upsert MySQL

MySQL. 8.0.19+ prefers aliases

INSERT INTO t (id, n) VALUES (1, 2)
ON DUPLICATE KEY UPDATE n = VALUES(n);

DDL

Create

Add constraints in the table, not later if you can

CREATE TABLE t (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
Index

B-tree by default

CREATE INDEX t_name_idx ON t (name);
Unique

Enforces uniqueness

CREATE UNIQUE INDEX t_email_uidx ON t (email);
Alter

Syntax varies a lot by engine

ALTER TABLE t ADD COLUMN bio TEXT;

Features

  • Searchable SQL syntax reference
  • Joins, data types, NULL, dates, strings, windows, indexes
  • Postgres, MySQL, and SQLite notes where they diverge
  • Copy any snippet
  • Runs locally, no account

Common Use Cases

  • Look up FULL OUTER JOIN syntax mid-query
  • Remember COALESCE vs IFNULL vs ISNULL
  • Copy a window-function skeleton
  • Check date truncation names across engines

SQL is a family of dialects

The core SELECT FROM WHERE shape is shared. Types, functions, and identifier quotes are not. Postgres ILIKE is MySQL LIKE with a case-insensitive collation. SQLite is typeless compared with the others. When a snippet is engine-specific, the sheet says so.

Examples

Valid - Limit
SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20;
Valid - SQL Server paging
SELECT * FROM t ORDER BY id OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

Frequently Asked Questions

Is this a full SQL standard reference?

No. It is a practical sheet for the queries people write daily. Use your engine docs for GRANT, replication, and vendor extensions.

Can I run these snippets here?

There is no database in the browser. Copy a snippet into your client, or pretty-print it in the SQL Formatter.

Tips

  • Search for COALESCE, OVER, or RETURNING if you know the keyword but forgot the shape.