1.3 CRUD Operations: INSERT, SELECT, UPDATE & DELETE Safely
Master the 4 everyday CRUD operations, bulk inserts, Upserts (ON CONFLICT), RETURNING, DISTINCT vs DISTINCT ON, and the transaction safety workflow that protects production databases.
1. The 4 Operations Behind Every Application (CRUD)
Every button inside a web or mobile app maps directly to one of 4 SQL operations:
| App Action | CRUD Letter | SQL Command | Example |
|---|---|---|---|
| User signs up / places an order | C (Create) | INSERT INTO | Add a new row to orders |
| User opens dashboard / profile | R (Read) | SELECT | Fetch rows from users & orders |
| User changes password / address | U (Update) | UPDATE ... SET ... WHERE | Edit specific column(s) of 1 row |
| User deletes a comment / item | D (Delete) | DELETE FROM ... WHERE | Remove specific row(s) |
2. Inserting Data (INSERT INTO, Bulk Inserts & Upserts)
Single vs Multi-Row Bulk Insert
Always list column names explicitly — never rely on positional column order! And when inserting multiple rows, 1 bulk INSERT is 10×–50× faster than running 100 individual INSERT statements because it avoids 99 network round-trips:
-- Single-row insert (explicit columns = resilient to future schema changes)
INSERT INTO students (id, name, course, score)
VALUES (101, 'Aarav', 'Python', 92);
-- Multi-row bulk insert (1 round-trip, 1 transaction!)
INSERT INTO students (id, name, course, score)
VALUES
(102, 'Meera', 'SQL', 96),
(103, 'Kabir', 'DSA', 88),
(104, 'Riya', 'SQL', 91);
Real-World Pro Pattern: Upsert ("Insert, or Update if It Already Exists")
What if a user clicks "Save Profile" twice or you're syncing data from an API? An Upsert inserts a new row if the key doesn't exist, or updates it cleanly if it does:
-- PostgreSQL / SQLite: ON CONFLICT ... DO UPDATE
INSERT INTO students (id, name, course, score)
VALUES (103, 'Kabir', 'SQL', 95)
ON CONFLICT (id)
DO UPDATE SET
course = EXCLUDED.course,
score = EXCLUDED.score;
-- MySQL equivalent: ON DUPLICATE KEY UPDATE
INSERT INTO students (id, name, course, score)
VALUES (103, 'Kabir', 'SQL', 95)
ON DUPLICATE KEY UPDATE
course = VALUES(course),
score = VALUES(score);
3. Reading Data (SELECT, Computed Columns, Aliases & DISTINCT)
-- 1. Select only the exact columns you need + compute expressions
SELECT
name AS student_name,
course,
score,
ROUND(score / 100.0 * 10, 1) AS gpa_scale
FROM students;
-- 2. Return unique courses only (deduplicates rows)
SELECT DISTINCT course
FROM students;
-- 3. Deduplicate by a COMBINATION of columns (unique (course, score) pairs)
SELECT DISTINCT course, score
FROM students;
SELECT * vs Explicit Columns (Why Senior Engineers Flag SELECT * in Code Reviews)
| Aspect | SELECT * | SELECT id, name, score |
|---|---|---|
| Network & Memory | Fetches huge unused TEXT/JSON blobs | Transfers only needed bytes |
| Index-Only Scans | Almost always forces expensive disk table lookups | Allows blazing-fast Index-Only Scans |
| Security | Can accidentally expose password_hash or PII to APIs | Explicit allowlist of safe fields |
| Best Used For | Quick ad-hoc exploration (LIMIT 5) | Every production query & API |
4. Updating & Deleting Rows — The Production Safety Workflow
-- Update specific columns for ONE exact row
UPDATE students
SET score = 95, course = 'SQL'
WHERE id = 103;
-- Delete ONLY rows matching a verified condition
DELETE FROM students
WHERE id = 104;
🚨 The #1 Production Disaster in SQL:
RunningUPDATE students SET score = 0;orDELETE FROM students;without aWHEREclause silently modifies or destroys EVERY SINGLE ROW IN THE TABLE!
The 3-Step Senior Engineer Safety Habit
Whenever you run a manual UPDATE or DELETE on a real database, wrap it in a transaction so you can verify the exact row count before committing:
BEGIN;
-- Step 1: Preview first!
SELECT * FROM students WHERE score < 90;
-- Step 2: Run the write (PostgreSQL RETURNING shows you the exact modified rows!)
DELETE FROM students
WHERE score < 90
RETURNING id, name, score;
-- Step 3: Look at the row count!
-- If it matches expectations -> COMMIT;
-- If you accidentally touched 50,000 rows -> ROLLBACK; (100% undone!)
COMMIT;
Watch CRUD Transform a Table Step-by-Step
| id | name | course | score |
|---|---|---|---|
| 101 | Aarav | Python | 92 |
| 102 | Meera | SQL | 96 |
| 103 | Kabir | DSA | 88 |
| 104 | Riya | SQL | 91 |
5. Quick Check Your Understanding
Module 1.3 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.