SELECT, INSERT, UPDATE, DELETE

beginner20 min

Learning objectives

  • Write SELECT queries with WHERE and ORDER BY
  • Insert, update and delete records safely
  • Explain why WHERE clauses matter for UPDATE/DELETE

Learn

The four core SQL operations

-- Read data
SELECT name, stock FROM products WHERE stock < 10;

-- Add a new record
INSERT INTO products (name, price, stock) VALUES ('USB Cable', 4.99, 50);

-- Change existing data
UPDATE products SET stock = stock - 1 WHERE id = 12;

-- Remove a record
DELETE FROM products WHERE id = 87;

The most important habit: always use WHERE

UPDATE and DELETE without a WHERE clause affect every row in the table. Running DELETE FROM products; with no WHERE deletes the entire products table's data — not one row.

Before running an UPDATE or DELETE in the SQL Lab, first run the equivalent SELECT with the same WHERE clause to check exactly which rows will be affected.

Try it yourself

Using the sample products dataset in the SQL Lab, find all products with fewer than 10 in stock, then write an UPDATE statement that increases one product's stock by 20.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab
Log in to track this lesson on your progress dashboard.
Log in