MySQL Cheat Sheet
A searchable, printable MySQL reference — queries, joins, indexes, functions, data types and administration commands. Free.
Querying
10SELECT * FROM users;
SELECT id, name FROM users;
SELECT DISTINCT city FROM users;
WHERE age > 18 AND active = 1
WHERE name LIKE 'A%'
WHERE id IN (1, 2, 3)
WHERE age BETWEEN 18 AND 30
ORDER BY created_at DESC
LIMIT 10 OFFSET 20
WHERE email IS NULL
Joins
8INNER JOIN orders ON orders.user_id = users.id
LEFT JOIN orders ON ...
RIGHT JOIN orders ON ...
CROSS JOIN colors
SELF JOIN: FROM emp e JOIN emp m ON e.mgr = m.id
USING (user_id)
UNION / UNION ALL
SELECT u.*, o.total FROM users u JOIN orders o
Aggregation
9COUNT(*)
SUM(amount)
AVG(price)
MIN(price), MAX(price)
GROUP BY country
HAVING COUNT(*) > 5
GROUP_CONCAT(name)
COUNT(DISTINCT city)
ROW_NUMBER() OVER (ORDER BY id)
Modifying data
8INSERT INTO users (name) VALUES ('Sam');
INSERT INTO users (name) VALUES ('A'),('B');
UPDATE users SET active = 1 WHERE id = 5;
DELETE FROM users WHERE id = 5;
TRUNCATE TABLE logs;
INSERT ... ON DUPLICATE KEY UPDATE
REPLACE INTO users ...
INSERT IGNORE INTO ...
Schema / DDL
9CREATE TABLE users (id INT PRIMARY KEY);
AUTO_INCREMENT
ALTER TABLE users ADD COLUMN age INT;
ALTER TABLE users DROP COLUMN age;
ALTER TABLE users MODIFY name VARCHAR(100);
DROP TABLE IF EXISTS users;
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE
CREATE TABLE t2 LIKE t1;
Indexes & keys
8PRIMARY KEY (id)
UNIQUE (email)
CREATE INDEX idx_name ON users (name);
CREATE INDEX idx_ab ON t (a, b);
DROP INDEX idx_name ON users;
FULLTEXT (body)
EXPLAIN SELECT ...;
SHOW INDEX FROM users;
Data types
9INT, BIGINT, TINYINT
DECIMAL(10, 2)
VARCHAR(255)
TEXT, LONGTEXT
DATE, DATETIME, TIMESTAMP
BOOLEAN (TINYINT(1))
JSON
ENUM('a', 'b')
UNSIGNED
Type sizes & limits
20TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
DECIMAL(M, D)
FLOAT / DOUBLE
CHAR(M)
VARCHAR(M)
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT
BLOB types
JSON
ENUM / SET
DATE
DATETIME
TIMESTAMP
TIME / YEAR
Functions
9NOW(), CURDATE()
DATE_FORMAT(d, '%Y-%m-%d')
DATEDIFF(a, b)
CONCAT(a, ' ', b)
COALESCE(a, b, 'n/a')
IFNULL(x, 0)
CASE WHEN x > 0 THEN '+' ELSE '-' END
CAST(x AS CHAR)
ROUND(x, 2)
Transactions & admin
9START TRANSACTION;
COMMIT;
ROLLBACK;
SAVEPOINT sp1;
SHOW TABLES;
DESCRIBE users;
SHOW PROCESSLIST;
GRANT ALL ON db.* TO 'u'@'%';
mysqldump -u root db > db.sql
No entry matches “:q”.
About MySQL Cheat Sheet
This MySQL cheat sheet gathers the SQL you actually type into one searchable reference: querying, joins, aggregation, modifying data, schema and DDL statements, indexes and keys, data types with their sizes and limits, built-in functions, and transactions and administration commands.
It is useful both as a syntax reminder — the exact shape of a LEFT JOIN, an UPSERT or an ALTER TABLE — and as a lookup for the details nobody memorizes, like integer ranges, VARCHAR limits and which index type fits a query.
Like every cheat sheet in this group it is free and client-side: filter rows live with the search box, hop between sections with the sticky table of contents, copy any statement with one click and print the page for reference at your desk.
How to use MySQL Cheat Sheet
- Open the sheet and review the sections, from Querying and Joins through Schema / DDL to Transactions & admin.
- Search for a keyword such as JOIN, INDEX or GROUP BY to filter all snippets live.
- Jump to Data types or Type sizes & limits when you need storage details rather than syntax.
- Click a statement or its copy icon to copy the SQL to your clipboard.
- Use Print for a paper copy of the full MySQL reference.