All tools
Free

A searchable, printable MySQL reference — queries, joins, indexes, functions, data types and administration commands. Free.

Querying

10
SELECT * FROM users;
Select all columns and rows
SELECT id, name FROM users;
Select specific columns
SELECT DISTINCT city FROM users;
Unique values only
WHERE age > 18 AND active = 1
Filter rows by conditions
WHERE name LIKE 'A%'
Pattern match (% = any chars)
WHERE id IN (1, 2, 3)
Match any value in a set
WHERE age BETWEEN 18 AND 30
Inclusive range filter
ORDER BY created_at DESC
Sort descending
LIMIT 10 OFFSET 20
Paginate results
WHERE email IS NULL
Match null values

Joins

8
INNER JOIN orders ON orders.user_id = users.id
Rows matching in both tables
LEFT JOIN orders ON ...
All left rows + matches
RIGHT JOIN orders ON ...
All right rows + matches
CROSS JOIN colors
Cartesian product
SELF JOIN: FROM emp e JOIN emp m ON e.mgr = m.id
Join a table to itself
USING (user_id)
Join on a same-named column
UNION / UNION ALL
Combine result sets
SELECT u.*, o.total FROM users u JOIN orders o
Alias tables for clarity

Aggregation

9
COUNT(*)
Count rows
SUM(amount)
Total of a numeric column
AVG(price)
Average value
MIN(price), MAX(price)
Smallest and largest
GROUP BY country
Group rows for aggregation
HAVING COUNT(*) > 5
Filter on aggregated groups
GROUP_CONCAT(name)
Concatenate grouped values
COUNT(DISTINCT city)
Count unique values
ROW_NUMBER() OVER (ORDER BY id)
Window function ranking

Modifying data

8
INSERT INTO users (name) VALUES ('Sam');
Insert a single row
INSERT INTO users (name) VALUES ('A'),('B');
Insert multiple rows
UPDATE users SET active = 1 WHERE id = 5;
Update matching rows
DELETE FROM users WHERE id = 5;
Delete matching rows
TRUNCATE TABLE logs;
Remove all rows quickly
INSERT ... ON DUPLICATE KEY UPDATE
Upsert on a unique key
REPLACE INTO users ...
Delete + insert on conflict
INSERT IGNORE INTO ...
Skip rows that would error

Schema / DDL

9
CREATE TABLE users (id INT PRIMARY KEY);
Create a new table
AUTO_INCREMENT
Auto-numbering column
ALTER TABLE users ADD COLUMN age INT;
Add a column
ALTER TABLE users DROP COLUMN age;
Remove a column
ALTER TABLE users MODIFY name VARCHAR(100);
Change a column type
DROP TABLE IF EXISTS users;
Delete a table
FOREIGN KEY (user_id) REFERENCES users(id)
Reference another table
ON DELETE CASCADE
Cascade deletes to children
CREATE TABLE t2 LIKE t1;
Copy a table structure

Indexes & keys

8
PRIMARY KEY (id)
Unique row identifier
UNIQUE (email)
Enforce unique values
CREATE INDEX idx_name ON users (name);
Speed up lookups on a column
CREATE INDEX idx_ab ON t (a, b);
Composite (multi-column) index
DROP INDEX idx_name ON users;
Remove an index
FULLTEXT (body)
Full-text search index
EXPLAIN SELECT ...;
Show the query plan
SHOW INDEX FROM users;
List indexes on a table

Data types

9
INT, BIGINT, TINYINT
Integer types by size
DECIMAL(10, 2)
Exact fixed-point numbers
VARCHAR(255)
Variable-length string
TEXT, LONGTEXT
Large text blobs
DATE, DATETIME, TIMESTAMP
Date and time types
BOOLEAN (TINYINT(1))
True/false stored as 0/1
JSON
Native JSON document column
ENUM('a', 'b')
One of a fixed set of values
UNSIGNED
Non-negative numeric modifier

Functions

9
NOW(), CURDATE()
Current datetime / date
DATE_FORMAT(d, '%Y-%m-%d')
Format a date
DATEDIFF(a, b)
Days between two dates
CONCAT(a, ' ', b)
Join strings
COALESCE(a, b, 'n/a')
First non-null value
IFNULL(x, 0)
Replace null with a default
CASE WHEN x > 0 THEN '+' ELSE '-' END
Conditional expression
CAST(x AS CHAR)
Convert a value type
ROUND(x, 2)
Round to decimals

Transactions & admin

9
START TRANSACTION;
Begin a transaction
COMMIT;
Persist the transaction
ROLLBACK;
Undo the transaction
SAVEPOINT sp1;
Set a rollback point
SHOW TABLES;
List tables in the database
DESCRIBE users;
Show a table structure
SHOW PROCESSLIST;
List running connections
GRANT ALL ON db.* TO 'u'@'%';
Grant user privileges
mysqldump -u root db > db.sql
Back up a database (CLI)

No entry matches “:q”.


Need help?
Found an issue with this tool? Let our team know.
Report an issue

Add this free tool to your own website — copy and paste the code below.