SQL · Module 01: SQL Foundations & Table DesignTutorial 3 of 24

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

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 ActionCRUD LetterSQL CommandExample
User signs up / places an orderC (Create)INSERT INTOAdd a new row to orders
User opens dashboard / profileR (Read)SELECTFetch rows from users & orders
User changes password / addressU (Update)UPDATE ... SET ... WHEREEdit specific column(s) of 1 row
User deletes a comment / itemD (Delete)DELETE FROM ... WHERERemove 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:

SQL
-- 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:

SQL
-- 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;
SQL
-- 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)

SQL
-- 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)

AspectSELECT *SELECT id, name, score
Network & MemoryFetches huge unused TEXT/JSON blobsTransfers only needed bytes
Index-Only ScansAlmost always forces expensive disk table lookupsAllows blazing-fast Index-Only Scans
SecurityCan accidentally expose password_hash or PII to APIsExplicit allowlist of safe fields
Best Used ForQuick ad-hoc exploration (LIMIT 5)Every production query & API

4. Updating & Deleting Rows — The Production Safety Workflow

SQL
-- 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:
Running UPDATE students SET score = 0; or DELETE FROM students; without a WHERE clause 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:

SQL
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

Tracing INSERT → UPDATE → DELETE → SELECT
Interactive SQL Execution Tracer · Step 1 of 4 (1. INSERT Row 104)
1→INSERT INTO students VALUES (104, 'Riya', 'SQL', 91);
2UPDATE students SET score = 95 WHERE id = 103;
3DELETE FROM students WHERE score < 92;
4SELECT name, score FROM students;
Live Table State · 1. INSERT Row 1044 rows
idnamecoursescore
101AaravPython92
102MeeraSQL96
103KabirDSA88
104RiyaSQL91
What the DB engine just did: INSERT adds the new record (104, Riya, SQL, 91) at the end of the table.

5. Quick Check Your Understanding

Module 1.3 Quick Quiz

Q1.What happens if you run UPDATE products SET price = 0; without a WHERE clause?
Q2.What does SELECT DISTINCT city, country FROM users; return?
Q3.What is an 'Upsert' (INSERT ... ON CONFLICT DO UPDATE)?

Keep Exploring: Related Lessons & Tools

All Courses →