Lesson 10 — Modifying Data

INSERT, UPDATE, and DELETE

While SELECT retrieves data, INSERT, UPDATE, and DELETE are the statements that add, change, and remove rows from a table.

INSERT

INSERT adds a new row, either by listing values matching the table's column order, or explicitly naming columns (the safer, more readable approach).

T-SQL — INSERT
INSERT INTO Customers (FirstName, LastName, Email)
VALUES ('Naledi', 'Mokoena', 'naledi@example.com');

UPDATE and DELETE

UPDATE modifies existing rows, and DELETE removes them entirely. Both are extremely dangerous without a WHERE clause — omitting it applies the change to every single row in the table.

T-SQL — UPDATE and DELETE
UPDATE Customers
SET Email = 'naledi.new@example.com'
WHERE CustomerId = 42;

DELETE FROM Orders
WHERE Status = 'Cancelled' AND OrderDate < '2024-01-01';

Common Mistakes

  • Running UPDATE or DELETE without a WHERE clause, accidentally affecting every row in the table.
  • Not testing the WHERE clause with a SELECT first to confirm exactly which rows will be affected before running the UPDATE/DELETE.
  • Forgetting to list column names in INSERT, relying on positional order that breaks silently if the table structure changes.
  • Running a destructive statement directly against a production database without a transaction or backup as a safety net.

Professional Tip

Before running any UPDATE or DELETE, run the same WHERE clause as a SELECT first: SELECT * FROM Orders WHERE Status = 'Cancelled' AND OrderDate < '2024-01-01'. Confirming exactly which rows match before modifying them is one of the most valuable habits in SQL.

Your Turn

Write an INSERT statement adding a new product, an UPDATE statement changing that product's price, and a DELETE statement removing it — each with explicit, safe WHERE clauses where relevant.

Mini Quiz

What happens if you run an UPDATE statement without a WHERE clause?