INSERT, UPDATE and DELETE
- INSERT. INSERT adds rows: with explicit column lists, so a reordered schema cannot silently shift your values.
- UPDATE with WHERE. UPDATE rewrites matching rows: and an UPDATE without WHERE rewrites the entire table, so look twice.
- DELETE carefully. DELETE removes matching rows: professionals SELECT with the same WHERE first to see what will go.
Upserts and Merge Patterns
- Upsert. An upsert: INSERT with ON CONFLICT: inserts a new row or updates the existing one atomically.
- Idempotent loads. Data loads built on upserts are idempotent: replaying yesterday’s file cannot double anything.
- RETURNING. The RETURNING clause hands back what a write produced: new ids without a second round trip.
Safe Data Changes
- Wrap in transactions. Risky changes run inside a transaction: verify the counts, then COMMIT: or ROLLBACK and nothing happened.
- Backfill in batches. Mass updates run in batches: a million-row single statement locks tables and bloats logs.
- Audit trails. Serious systems record who changed what: an audit table or soft deletes preserving history.
Before and after this level
- Before: Level 6: Window Functions: Ranking and Partitioning, Running and Moving Calculations, Advanced Analytical Queries.
- Next: Level 8: Schema Design: Tables and Data Types, Keys and Constraints, Normalisation and Relationships.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does SQL Level 7: Data Modification cover?
INSERT, UPDATE and DELETE, Upserts and Merge Patterns, Safe Data Changes: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 6: Window Functions, which covers Ranking and Partitioning, Running and Moving Calculations, Advanced Analytical Queries.