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 minor dialect differences. Keywords are shown in uppercase by convention, but SQL is 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 |
... WHERE a > 10; | Filter rows by a condition |
... ORDER BY a DESC; | Sort the results |
... LIMIT 10 OFFSET 20; | Return a page of rows |
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 ... | All left rows, plus matches (NULLs if none) |
RIGHT JOIN ... | All right rows, plus matches |
FULL JOIN ... | All rows from both sides |
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 |
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.
Sources & methodology
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 and data types differ between dialects.
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.