Module 6 · Changing data

Update rows with UPDATE

By the end you will be able to change values of the chosen rows without altering the others.

About 12 min

0 of 52 lessons

Before you start, you need

Add rows with INSERT

Update the attributes listed in SET

An order already exists, but now you know it has been shipped. You do not need to create another order: UPDATE changes the values of the existing row and keeps its identity.

SET says which column changes and its new value. WHERE says which rows to change. SET status = 'shipped' WHERE id = 102 changes the status of order 102 and leaves the other orders as they were.

Columns not named in SET keep their value. Changing the status does not change the amount or the customer. Before updating, query the row with the same condition to make sure you have picked the right one.

Afterwards, query it again to check the change. Without WHERE, every row would be updated. A statement running without errors does not prove that only what the question asked for has changed.

Minimal syntax
UPDATE table_name
SET column_name = value
WHERE condition;

See it in action

Order 102 has already been shipped. We want to change only its status.

SQL
SELECT id, status
FROM orders
WHERE id = 102;

UPDATE orders
SET status = 'shipped'
WHERE id = 102;

SELECT id, status
FROM orders
WHERE id = 102;
Example result: 1 rows
idstatus
102shipped
  1. The SELECT beforehand shows the target row. Its id identifies the order without depending on the status that is about to change.
  2. SET status = 'shipped' changes only that attribute. WHERE id = 102 limits the scope to that order.
  3. The final check shows the new status. The order's amount and customer stay as they were.
  4. Leaving out WHERE would update every order. Checking the right row and keeping the others are part of the same task.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Update order 102

Not started

Write the SQL statement that updates only order 102 so that its status is shipped.

We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Update another order

Not started

Change only the status of order 105 to 'shipped'. Query id and status before and after the change.

We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Key takeaways

  • UPDATE changes existing rows.
  • SET lists the attributes that change.
  • WHERE limits the scope.
  • Columns not listed keep their values.

Next: Delete rows with DELETE