Skip to content

Beginner · Databases and SQL basics · topic 19 of 54 · 25 min

INSERT, UPDATE and DELETE

Explanation

INSERT adds new rows, UPDATE changes rows that already exist, and DELETE removes rows. UPDATE and DELETE need a WHERE clause to choose the rows, or they change every row in the table.

Adding rows with INSERT

INSERT INTO is followed by the table name, the list of columns, and VALUES with the data in the same order. Naming the columns is safer than relying on the table's column order, because tables change over time.

sql
INSERT INTO orders (id, customer, city, amount, status)
VALUES (7, 'Meena', 'Jaipur', 999, 'Placed');

-- many rows in one statement
INSERT INTO orders (id, customer, city, amount, status)
VALUES (8, 'Sunil', 'Indore', 450, 'Placed'),
       (9, 'Fatima', 'Lucknow', 1299, 'Placed');
One row, then two rows at once. Text in single quotes, numbers without.

Changing rows with UPDATE

UPDATE table SET column = new value WHERE condition. SET can change several columns, separated by commas, and the new value can use the old one, like amount = amount + 50.

sql
UPDATE orders
SET status = 'Shipped'
WHERE id = 3;
Only order 3 changes; every other row stays the same.

Removing rows with DELETE

sql
DELETE FROM orders
WHERE status = 'Cancelled';
Removes only the cancelled orders.

The danger of forgetting WHERE

Think of it like this: UPDATE without WHERE is like a teacher who wants to mark one student absent but shouts "everyone is absent!" in the register. Every single row gets the change. There is no undo button in SQL.
Careful: UPDATE orders SET status = 'Cancelled'; with no WHERE cancels EVERY order. DELETE FROM orders; with no WHERE empties the table. Before running an UPDATE or DELETE, first run a SELECT with the same WHERE and check that it returns only the rows you mean to change.
Tip: Change rows by their primary key (WHERE id = 3) whenever you can. A primary key matches exactly one row, so you cannot hit others by mistake.

Seeing what changed: RETURNING

PostgreSQL and SQLite (since version 3.35) support RETURNING at the end of INSERT, UPDATE or DELETE. It gives back the changed rows, which is handy in an API: INSERT ... RETURNING id tells you the id of the new row. In the tasks below we use a separate SELECT after the change instead, so you can see the whole table.

sql
INSERT INTO orders (customer, city, amount, status)
VALUES ('Ritu', 'Bhopal', 640, 'Placed')
RETURNING id;
The database picks the next id and returns it.

Watch the lesson

INSERT, UPDATE and DELETE · 1/6

INSERT, UPDATE, DELETE

Reading data is half the job. Today we add, change and remove rows.

Prefer a video by a teacher? Find free videos on “sql insert update delete tutorial for beginners” on YouTube ↗

In real life: An order's life in a food delivery app

When you place an order, the backend runs an INSERT to add a row with status Placed. When the restaurant accepts it, an UPDATE sets status = 'Preparing' WHERE id = your order id; later another UPDATE sets it to 'Out for delivery' and then 'Delivered'. Many apps do not really DELETE old orders; they keep them for bills and tax records and only mark them cancelled. Real deletes are kept for things like removing a user's data when they close their account.

Practice (0/3 done)

Task 1 · Code · SQL

Add a new order to the orders table (columns id, customer, city, amount, status): id 7, customer Meena, city Jaipur, amount 999, status Placed. The SELECT at the end is already written: it shows the new row so the checker can see it. Write your INSERT above it.

Task 2 · Code · SQL

Priya's order (id 3) has left the warehouse. Change its status to Shipped. Change ONLY that order. The SELECT at the end is already written: it shows every order's status after your change.

Task 3 · Code · SQL

Remove every order whose status is Cancelled, and nothing else. The SELECT at the end is already written: it lists the orders that are left.

Quick quiz

Question 1

1.What happens with UPDATE products SET price = 0; (no WHERE)?

Question 2

2.Which statement adds a new row?

Question 3

3.What is the safest WHERE for changing one order?

Question 4

4.What does RETURNING id do at the end of an INSERT?

Question 5

5.A good habit before running a DELETE is to...

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: ORDER BY, LIMIT and DISTINCT