A SQL cheat sheet is the language of relational databases: SELECT columns from tables, filter with WHERE, combine with JOIN, aggregate with GROUP BY, and write with INSERT/UPDATE/DELETE. Indexes speed lookups at the cost of writes. The ideas transfer across MySQL, PostgreSQL, and SQL Server; dialects differ on functions and types.
This SQL cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- SELECT:
SELECT id, email FROM users— list columns, avoidSELECT *in app code. - WHERE: Filter rows.
AND/OR,IN,BETWEEN,LIKE,IS NULL. - ORDER / LIMIT:
ORDER BY created_at DESC LIMIT 20 OFFSET 0. - JOIN:
INNER JOINmatches both sides.LEFT JOINkeeps the left table. - GROUP BY: Aggregates (
COUNT,SUM) per group.HAVINGfilters groups. - INSERT:
INSERT INTO t (a, b) VALUES (1, "x"). - UPDATE/DELETE: Always
WHERE. Start with aSELECTof the same predicate. - Index:
INDEX (email)for lookups and joins. PrefixLIKE "ab%"can use an index;%abcannot.
Copy-paste examples
Filter, join, and aggregate
Qualify column names when two tables share id. Aliases keep the query readable.
SELECT p.id, p.title, COUNT(c.id) AS comment_count
FROM posts AS p
LEFT JOIN comments AS c ON c.post_id = p.id
WHERE p.published = 1
GROUP BY p.id, p.title
ORDER BY p.id DESC
LIMIT 20;Common mistakes
DELETE FROM userswithoutWHERE(or with a buggy predicate) in production.SELECT *in hot API paths and shipping extra blobs over the wire.- Filtering in PHP after fetching the whole table.
- Indexing every column “just in case” and slowing writes.
FAQ
- Is SQL a programming language? It is a query language for sets of rows. You still use PHP/Python/etc. for application logic. Many job posts still say “SQL programming.”
- MySQL vs SQL? SQL is the language. MySQL is a database product that implements a dialect of SQL.
- How do I prevent SQL injection? Parameterized queries (PDO, prepared statements). Never concatenate user input into SQL strings.
Related SQL cheat sheets
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds custom web development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.