The MySQL Cheat Sheet: Queries You'll Actually Use

Table of Contents
CREATE DATABASE
CREATE DATABASE
FOREIGN KEY
FOREIGN KEY
GROUP BY
GROUP BY
INNER JOIN
INNER JOIN
LEFT JOIN
LEFT JOIN
When you start working with databases, MySQL is usually the first system you'll encounter. It's battle-tested and powers everything from small side projects to massive enterprise applications.
Instead of reading a massive textbook, this guide covers the 20% of commands you'll use 80% of the time!
Quick Start
Don't have MySQL installed? Spin it up instantly using Docker:
docker run --name mysql-dev -e MYSQL_ROOT_PASSWORD=secret -d mysql
1. Databases & Tables
Manage Databases
Connect via terminal with mysql -u root -p.
Create and select a database:
CREATE DATABASE zoo_db;
USE zoo_db;
Create Tables with Relationships
Use Foreign Keys to ensure data integrity!
CREATE TABLE habitat (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(64));
CREATE TABLE animal (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(64),
habitat_id INT,
FOREIGN KEY (habitat_id) REFERENCES habitat(id)
);
2. Data Manipulation (CRUD)
| Operation | Syntax | Key Point |
|---|---|---|
| INSERT | INSERT INTO table (col) VALUES (val) | Auto-increment ID handled automatically |
| UPDATE | UPDATE table SET col=val WHERE condition | ALWAYS use WHERE clause! |
| DELETE | DELETE FROM table WHERE condition | Without WHERE = delete all rows! |
| SELECT | SELECT col1, col2 FROM table WHERE condition | * = all columns (avoid in production) |
INSERT INTO habitat (name) VALUES ('River'), ('Forest');
UPDATE animal SET name = 'Quack' WHERE id = 2;
DELETE FROM animal WHERE id = 1;
Always use WHERE!
If you run an UPDATE or DELETE command without a WHERE clause, you will modify or delete every single row in the table. Always double-check your queries!
3. Querying and Aggregation
The Power of GROUP BY
| Clause | Purpose | Example |
|---|---|---|
| SELECT | Columns to return | species, AVG(age) |
| WHERE | Filter rows BEFORE grouping | WHERE id != 3 |
| GROUP BY | Group rows by column | GROUP BY species |
| HAVING | Filter groups AFTER aggregation | HAVING AVG(age) > 3 |
| ORDER BY | Sort final result | ORDER BY AVG(age) DESC |
Want to find the average age of each animal species?
SELECT species, AVG(age) as average_age
FROM animal
WHERE id != 3
GROUP BY species
HAVING AVG(age) > 3
ORDER BY AVG(age) DESC;
This powerful query filters, groups, aggregates, filters the groups, and sorts the final result!
Joining Tables
| Join Type | Result | Use When |
|---|---|---|
| INNER JOIN | Only matching rows from both | Required relationships |
| LEFT JOIN | All left + matched right (NULL if no match) | Optional relationships |
| RIGHT JOIN | All right + matched left (NULL if no match) | Rarely used (flip tables instead) |
| CROSS JOIN | Cartesian product (all combinations) | Generating test data |
To combine data from our two tables and view the animal's name alongside its habitat's name:
SELECT animal.name AS animal_name, habitat.name AS habitat_name
FROM animal
INNER JOIN habitat ON animal.habitat_id = habitat.id;
Practice Makes Perfect
Mastering MySQL requires muscle memory. Open up your terminal and practice creating tables and joining data until it becomes second nature!
Related Posts
- REST API vs GraphQL — When to use which for your data layer
- Modern Python Development Environment — Python database tooling
You Might Also Like
Free In-Browser Developer Tools
Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.
Related Articles

PostgreSQL Vacuum & Index Bloat: Detection, Mitigation, and Automated Tuning
Diagnose and eliminate PostgreSQL table and index bloat. Master autovacuum tuning formulas, pg_repack zero-downtime compaction, and MVCC visibility maps.
Read more
SQLite in Production: WAL Mode, High Concurrency, and Battle-Tested PRAGMAs
Master SQLite in high-throughput production environments. Learn Write-Ahead Logging (WAL), busy timeout tuning, concurrent reader/writer limits, and pragmatic benchmarks.
Read more
AI Agent Memory Architectures: Vector Store Integration
Architectural guide for building multi-tier AI agent memory systems using short-term rolling windows, long-term vector stores, and state persistence.
Read more