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

The most common retrieval patterns developers use every day.

Select all columns

Return all columns from a table.

sqlANYselectall
sql
SELECT * FROM users;

Convenient for quick exploration, but explicit column lists are usually better in production queries.

Select specific columns

Return only the columns you need.

sqlANYselectcolumns
sql
SELECT id, email, full_name FROM users;

Reducing selected columns helps readability and can reduce network overhead.

Filter rows with WHERE

Match rows using equality conditions.

sqlANYwherefilter
sql
SELECT id, email FROM users WHERE is_active = 1;

`WHERE` narrows the result set before ordering and limiting.

Sort and limit results

Return the newest rows first.

sqlANYorder-bylimit
sql
SELECT id, email, created_at FROM users ORDER BY created_at DESC LIMIT 20;

A common pattern for feeds, admin lists, and dashboards.

Return distinct values

Collapse duplicates in a result set.

sqlANYdistinctstatus
sql
SELECT DISTINCT status FROM orders ORDER BY status;

Useful for reporting, filters, and data exploration.

Filter by date range

Return rows between two timestamps.

sqlANYbetweendatesreporting
sql
SELECT id, created_at FROM orders WHERE created_at BETWEEN '2026-01-01' AND '2026-01-31 23:59:59';

Use explicit endpoints to avoid off-by-one issues in reporting.

insert, update, and delete

Modify data safely with common write patterns.

Insert one row

Create a new row with explicit columns.

sqlANYinsertcreate
sql
INSERT INTO users (email, full_name) VALUES ('ava@example.com', 'Ava Stone');

Always listing target columns makes inserts safer during schema changes.

Insert multiple rows

Create several rows in one statement.

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

Batch inserts reduce round trips and are often much faster than many single-row statements.

Update matching rows

Change values for rows that match a filter.

sqlANYupdatewhere
sql
UPDATE users SET is_active = 0 WHERE email LIKE '%@old-domain.com';

Always include a restrictive `WHERE` clause unless you intentionally want to update every row.

Delete matching rows

Remove rows that match a predicate.

sqlANYdeletecleanup
sql
DELETE FROM sessions WHERE expires_at < NOW();

A classic cleanup task for expired sessions or temporary data.

Truncate a table

Remove all rows quickly and reset auto-increment counters.

sqlANYtruncatestaging
sql
TRUNCATE TABLE staging_events;

Faster than `DELETE` for clearing a whole table, but behaves differently with transactions and foreign keys.

Upsert with ON DUPLICATE KEY UPDATE

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

sqlANYupsertduplicate-key
sql
INSERT INTO users (email, full_name)
VALUES ('ava@example.com', 'Ava Stone')
ON DUPLICATE KEY UPDATE full_name = VALUES(full_name);

A classic MySQL upsert pattern for unique-keyed rows.

Replace a row using REPLACE

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

sqlANYreplaceupsert
sql
REPLACE INTO feature_flags (flag_key, flag_value) VALUES ('beta_ui', 'on');

`REPLACE` can have surprising semantics because it may delete the old row and insert a new one.

Recommended next

No recommendations yet.