SQL Cheat Sheet
A reference for standard SQL, querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data and schema.
A reference for standard SQL, the syntax is shared across PostgreSQL, MySQL, SQLite, and SQL Server, with dialect differences noted below. Keywords are shown in uppercase by convention; SQL keywords are case-insensitive.
Querying data
| Statement | What it does |
|---|---|
SELECT * FROM t; | Return every column and row |
SELECT a, b FROM t; | Return specific columns |
SELECT DISTINCT a FROM t; | Return unique values |
SELECT * FROM t WHERE a > 10; | Filter rows by a condition |
SELECT * FROM t ORDER BY a DESC; | Sort the results |
SELECT * FROM t LIMIT 10 OFFSET 20; | Return a page of rows (PostgreSQL, MySQL, SQLite; not SQL Server) |
SELECT * FROM t ORDER BY a OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY; | Standard paging; works in SQL Server and PostgreSQL. SQL Server also has SELECT TOP (10) * FROM t; |
Filtering (WHERE)
| Operator | Matches |
|---|---|
= <> < > | Equal, not equal, less / greater than |
BETWEEN 1 AND 9 | Within an inclusive range |
IN ('a','b') | Matches any value in a list |
LIKE 'a%' | Pattern match (% = any, _ = one char) |
IS NULL | Field has no value |
AND / OR / NOT | Combine conditions |
Aggregates & grouping
| Function / clause | What it does |
|---|---|
COUNT(*) | Number of rows |
SUM(a) AVG(a) | Total / average of a column |
MIN(a) MAX(a) | Smallest / largest value |
GROUP BY a | Aggregate per distinct value of a |
HAVING COUNT(*) > 1 | Filter groups (like WHERE, but post-aggregate) |
Joining tables
| Join | Returns |
|---|---|
INNER JOIN u ON u.id = t.uid | Only rows matching in both tables |
LEFT JOIN u ON u.id = t.uid | All left rows, plus matches (NULLs if none) |
RIGHT JOIN u ON u.id = t.uid | All right rows, plus matches (SQLite 3.39.0+) |
FULL JOIN u ON u.id = t.uid | All rows from both sides, NULLs where unmatched. Not in MySQL (combine a LEFT and a RIGHT JOIN with UNION); SQLite 3.39.0+ |
Modifying data & tables
| Statement | What it does |
|---|---|
INSERT INTO t (a,b) VALUES (1,2); | Add a new row |
UPDATE t SET a=1 WHERE id=5; | Change existing rows |
DELETE FROM t WHERE id=5; | Remove rows |
CREATE TABLE t (id INT PRIMARY KEY, a TEXT); | Create a table |
ALTER TABLE t ADD COLUMN c INT; | Add a column. SQL Server has no COLUMN keyword: ALTER TABLE t ADD c INT; |
CREATE INDEX idx ON t (a); | Speed up lookups on a column |
Always pair UPDATE and DELETE with a
WHERE clause, without one they affect every row. Test with a
SELECT using the same WHERE first.
What this does
A SQL cheat sheet covers standard SQL — querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data.
How to use it
- Browse by clause.
- Find the syntax you need.
- Copy it with one tap.
- Swap in your table and column names.
Example
SELECT * FROM users WHERE active = 1 returns every active user (with active stored as a 0/1 number).
Last updated .
Frequently asked questions
Does this apply to MySQL and PostgreSQL?
Yes, the core syntax is shared across PostgreSQL, MySQL, SQLite, and SQL Server. Some functions, data types and clauses differ: SQL Server pages with TOP or OFFSET/FETCH instead of LIMIT, MySQL has no FULL JOIN, and the sheet notes each of these.
What’s the difference between WHERE and HAVING?
WHERE filters individual rows before grouping; HAVING filters groups after aggregation. Use HAVING with aggregate conditions like COUNT(*) > 1.
How do I avoid deleting every row?
Always pair UPDATE and DELETE with a WHERE clause. Test the same WHERE with a SELECT first to confirm exactly which rows are affected.
Related tools
Git Cheat Sheet
The Git commands you use every day, setup, the add/commit/push workflow, branching and merging, working with remotes, and undoing mistakes safely.
Docker Cheat Sheet
A reference for everyday Docker, building and tagging images, running and managing containers, ports and volumes, system cleanup, and Docker Compose.
Vim Cheat Sheet
A reference for the Vim editor, modes, moving around, editing, search and replace, and saving or quitting, including the escape hatch for when you are stuck.
Regex Cheat Sheet
A regular-expression reference, character classes, anchors, quantifiers, groups and alternation, lookarounds, common ready-made patterns, and flags.