Quick Reference · relational database & SQL

mysql cheat sheet 8.0 / 8.4 LTS

Every statement either defines structure (DDL), changes data (DML), reads data (DQL), or controls the server (DCL/TCL). MySQL 8.0 layered analytical SQL on top — window functions, CTEs, native JSON, and roles. Cards tagged 8.0+ mark features unavailable in 5.7.

connect / inspect structure (DDL) data (DML) query (DQL) admin (DCL/TCL) modern SQL (8.0+) destructive most common

Distilled & cross-checked against: dev.mysql.com/doc/refman/8.0 · What's New in MySQL 8.0 · MySQL 8.4 LTS notes · mysqltutorial.org · learnsql.com · quickref.me

Anatomy of a SELECT — the order you write it vs. the order MySQL runs it
YOU WRITE ↓ SELECT FROM/JOIN WHERE GROUP BY HAVING WINDOW8.0+ ORDER BY LIMIT MYSQL RUNS ↓ (this order decides what's valid & what's fast) 1· FROM+ JOIN 2· WHEREfilters rows 3· GROUP BYbuckets 4· HAVINGfilters groups 5· WINDOWper-row calc 6· SELECT+ DISTINCT 7· ORDERthen LIMIT Why this order matters • WHERE can't reference a SELECT alias — SELECT runs at step 6, long after WHERE at step 2. • Filter with WHERE before grouping (cheap); filter with HAVING after grouping, on aggregates (expensive). • Window functions (step 5) see the grouped/filtered set but run before ORDER BY — so you can ORDER BY a window result. • ORDER BY / LIMIT run last — MySQL sorts & pages only once every earlier stage is resolved.
01Connect & Sessioncommand-line client
02DatabasesDDL
03Create & Inspect TablesDDL
04Alter Tablereshape structure
05Data Typeswhat a column can hold

Numeric

TINYINT1B · ±127 (0–255 unsigned)
SMALL/MEDIUM/INT2 / 3 / 4 bytes
BIGINT8 bytes · ±9.2×10¹⁸
DECIMAL(M,D)exact fixed-point — use for money
FLOAT / DOUBLEapproximate floating-point

String

CHAR / VARCHAR(M)fixed / variable length
TEXT / LONGTEXT64KB → 4GB of text
ENUM / SETone / many from a fixed list
BLOB / LONGBLOBbinary, same size tiers as TEXT

Date & Time

DATE / TIME / YEARcalendar parts
DATETIMEYYYY-MM-DD HH:MM:SS, no TZ shift
TIMESTAMPUTC-stored, 1970–2038

Other & 8.0

BOOLEANalias for TINYINT(1)
JSONnative, validated, binary-stored (5.7+)
GEOMETRY / POINTspatial, SRID-aware in 8.0
06Insert DataDML
07Select — BasicsDQL
08Filter Rows — WHEREDQL
09Sort & PaginateDQL
10JoinsDQL · combine tables
11Group & AggregateDQL · roll rows up
12Set OperationsDQL · combine result sets
13CTEs 8.0+WITH · named subqueries
14Window Functions 8.0+per-row calc over a window
15JSON 8.0+native document handling
16Regular Expressions 8.0+ICU engine, full Unicode
17Full-Text SearchInnoDB & MyISAM
18Common Functionsstring / date / numeric / flow
19Update & DeleteDML · handle with care
20Keys & ConstraintsDDL · data integrity
21IndexesDDL · speed up reads
22Generated ColumnsDDL · computed columns
23PartitioningDDL · split one table physically
24TransactionsTCL · InnoDB only
25Locking Reads 8.0+DQL · concurrency
26Users & PrivilegesDCL · access control
27Roles 8.0+DCL · grouped privileges
28ViewsDDL · saved queries
29Procedures & Triggersserver-side logic
30Backup, Restore & Importshell · not run inside mysql>
31Optimize & Inspectperformance tuning
Operators & Wildcardsused throughout

MySQL 8.0's flagship additions, visualized

The three features that most change how you write MySQL — from the official What's New in MySQL 8.0 reference.

GROUP BY vs. window function

GROUP BY collapses rows into one per group; a window function computes per group but keeps every row.

GROUP BY A 10 A 20 B 30 B 40 A 30 B 70 OVER(PARTITION BY) A 10 → 30 A 20 → 30 B 30 → 70 B 40 → 70 all 4 rows kept

Recursive CTE walks a tree

The base row seeds the set; each pass joins children on, until no new rows appear — depth accumulates as level.

CEO VP-A VP-B Eng Ops level 1 level 2 level 3

JSON_TABLE bridges to SQL

A JSON array column becomes relational rows — then joins, filters, and aggregates like any table.

[ {id:1,  nm:"A"}, {id:2,  nm:"B"} ] JSON column JSON_TABLE id nm 1 A 2 B relational rows

Join types at a glance

Same two tables, five result sets. Shaded = rows returned.

INNER JOIN

Only rows where the key exists on both sides.

a b

LEFT JOIN

All of the left table, matched right rows or NULL.

a b

RIGHT JOIN

All of the right table, matched left rows or NULL.

a b

FULL OUTER (via UNION)

Everything from both sides. MySQL has no native FULL JOIN — LEFT UNION RIGHT.

a b

CROSS JOIN

Cartesian product — every row of a paired with every row of b.

a1 a2 a3 b1 b2 × 3 × 2 = 6 result rows

Worth memorizing

DELETE ≠ TRUNCATEDELETE is row-logged & rollback-able; TRUNCATE wipes instantly & can't
WHERE ≠ HAVINGWHERE filters rows before grouping; HAVING filters groups after
GROUP BY ≠ windowOVER() aggregates per group but keeps every row; GROUP BY collapses them
PRIMARY KEY ≠ UNIQUEPK forbids NULL, one per table; UNIQUE allows one NULL, many per table
CTE ≠ subquerya CTE can be referenced multiple times & can recurse; a derived table can't
EXPLAIN vs ANALYZEEXPLAIN estimates the plan; EXPLAIN ANALYZE runs it & times it
roles > per-user grantsgrant to a role once, assign the role — central privilege mgmt (8.0)
invisible indextest whether dropping an index hurts, without actually dropping it (8.0)
-> vs ->>-> returns JSON; ->> also unquotes to a plain string
SKIP LOCKEDthe clean way to build a concurrent job queue on a table (8.0)
utf8mb4 default8.0's default charset — true 4-byte Unicode, real emoji support
no query cacheremoved in 8.0 — rely on the buffer pool & app-side caching