4.4.4 · D3Databases

Worked examples — SQL DML — SELECT, INSERT, UPDATE, DELETE

3,881 words18 min readBack to topic

Everything below runs on the same running table from the parent, employees, freshly loaded with these four rows every time an example says "start fresh":

id name dept salary
1 Asha Sales 50000
2 Ben Eng 70000
3 Cira Eng 90000
4 Dia Sales 40000

See the parent topic for the table's CREATE statement.


The scenario matrix

Before working examples, let's list every case class a beginner can hit. A "case class" is a distinct type of surprise — the sign flips, the empty results, the edge conditions. Think of each row as a cell we must cover.

# Case class The trap it hides
C1 Normal filtered SELECT baseline — a predicate that keeps some rows
C2 Empty result (predicate matches nothing) 0 rows is a valid answer, not an error
C3 The NULL degenerate case = NULL never matches; you need IS NULL
C4 Aggregate over an empty / grouped set COUNT returns 0, but AVG/SUM return NULL
C5 UPDATE that references the old value right-hand side = value before the write
C6 Order-of-operations chain (UPDATE then DELETE) the second statement sees the new data
C7 The missing-WHERE catastrophe one statement changes every row
C8 DELETE vs TRUNCATE limiting behaviour filtered+undoable vs all+fast
C9 Real-world word problem translate English → predicate
C10 Exam twist: alias in WHERE / logical order why the query errors

Each example below is tagged with the cell(s) it covers. Between them, every cell is hit.


Example 1 — Normal filtered SELECT [C1]


Example 2 — Empty result set [C2]


Example 3 — The NULL degenerate case [C3]


Example 4 — Aggregates over groups and emptiness [C4]

The figure below shows the mechanism as a left-to-right pipeline with three labelled stages. Stage 1 (left): the four raw rows exactly as the table holds them — each row's dept and salary visible. Stage 2 (middle arrows): GROUP BY dept routes each row into its box — follow the black arrow that carries Asha 50000 and Dia 40000 into the Sales box, and the red arrows that carry Ben 70000 and Cira 90000 into the Eng box. Stage 3 (right boxes): each box lists its two salaries and then the single AVG number they collapse to. The red Eng box is the key object — watch its two inputs (70000, 90000) become the one output 80000.

Figure — SQL DML — SELECT, INSERT, UPDATE, DELETE

Example 5 — UPDATE that references the old value [C5]


Example 6 — Order-of-operations chain: UPDATE then DELETE [C6]


Example 7 — The missing-WHERE catastrophe [C7]


Example 8 — DELETE vs TRUNCATE: the limiting behaviour [C8]


Example 9 — Real-world word problem [C9]


Example 10 — Exam twist: alias in WHERE, and logical order [C10]


Coverage check

Recall Did we hit every matrix cell?

C1 ::: Example 1 (filtered SELECT) C2 ::: Example 2 (empty result set) C3 ::: Example 3 (= NULL vs IS NULL) C4 ::: Example 4 (COUNT=0 but AVG=NULL on empty; grouped averages) C5 ::: Example 5 (UPDATE using old value, * 1.10) C6 ::: Example 6 (UPDATE-then-DELETE chain sees new data) C7 ::: Example 7 (missing-WHERE hits all rows) C8 ::: Example 8 (DELETE filtered+undoable vs TRUNCATE all+fast) C9 ::: Example 9 (English word problem → predicate) C10 ::: Example 10 (alias in WHERE errors; logical order)


Active Recall

What truth value does salary = NULL produce, and which rows does WHERE keep?
It produces UNKNOWN; WHERE keeps only TRUE rows, so = NULL matches nothing — use IS NULL.
On an empty input set, what do COUNT(*) and AVG(x) each return?
COUNT(*) returns 0; AVG (and SUM/MIN/MAX) return NULL.
In SET salary = salary * 1.10, which value does the right-hand salary use?
The row's current value before the update.
In an UPDATE-then-DELETE sequence, does the DELETE see old or new values?
New values — statements run in order, so DELETE reads what UPDATE already wrote.
Why does WHERE annual > 600000 fail when annual is a SELECT-list alias?
WHERE runs before SELECT, so the alias doesn't exist yet.
DELETE with a WHERE vs TRUNCATE — rows affected and reversibility?
DELETE removes only filtered rows and is rollback-able; TRUNCATE empties all rows fast and is usually not rollback-able.

Connections

  • 4.4.04 SQL DML — SELECT, INSERT, UPDATE, DELETE (Hinglish) — the parent this deep-dive extends.
  • WHERE clause and Predicates — the filter behind Examples 1, 3, 9.
  • Aggregate Functions & GROUP BY — the empty-set edge case of Example 4.
  • Transactions — ACID, COMMIT, ROLLBACK — the recovery drill for Example 7.
  • SQL DDL — CREATE, ALTER, DROP — TRUNCATE's DDL nature in Example 8.
  • SQL Joins · Indexes — where these SELECT patterns scale to many tables.