MySQL SELECT, INSERT, UPDATE, and DELETE Cheatsheet

practical MySQL CRUD queries including filtering, sorting, pagination basics, inserts, updates, deletes, truncation, and upsert patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## basic selects
Select all columns
SELECT * FROM users;

# Return all columns from a table.

Select specific columns
SELECT id, email, full_name FROM users;

# Return only the columns you need.

Filter rows with WHERE
SELECT id, email FROM users WHERE is_active = 1;

# Match rows using equality conditions.

Sort and limit results
SELECT id, email, created_at FROM users ORDER BY created_at DESC LIMIT 20;

# Return the newest rows first.

Return distinct values
SELECT DISTINCT status FROM orders ORDER BY status;

# Collapse duplicates in a result set.

Filter by date range
SELECT id, created_at FROM orders WHERE created_at BETWEEN '2026-01-01' AND '2026-01-31 23:59:59';

# Return rows between two timestamps.

## insert, update, and delete
Insert one row
INSERT INTO users (email, full_name) VALUES ('ava@example.com', 'Ava Stone');

# Create a new row with explicit columns.

Insert multiple rows
INSERT INTO users (email, full_name) VALUES
  ('liam@example.com', 'Liam Cole'),
  ('mia@example.com', 'Mia Shaw');

# Create several rows in one statement.

Update matching rows
UPDATE users SET is_active = 0 WHERE email LIKE '%@old-domain.com';

# Change values for rows that match a filter.

Delete matching rows
DELETE FROM sessions WHERE expires_at < NOW();

# Remove rows that match a predicate.

Truncate a table
TRUNCATE TABLE staging_events;

# Remove all rows quickly and reset auto-increment counters.

Upsert with ON DUPLICATE KEY UPDATE
INSERT INTO users (email, full_name)
VALUES ('ava@example.com', 'Ava Stone')
ON DUPLICATE KEY UPDATE full_name = VALUES(full_name);

# Insert a row or update it when a unique key already exists.

Replace a row using REPLACE
REPLACE INTO feature_flags (flag_key, flag_value) VALUES ('beta_ui', 'on');

# Insert or delete-and-insert based on unique key conflicts.

Recommended next

No recommendations yet.