Before you start, you need
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.
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.
SELECT id, status
FROM orders
WHERE id = 102;
UPDATE orders
SET status = 'shipped'
WHERE id = 102;
SELECT id, status
FROM orders
WHERE id = 102;| id | status |
|---|---|
| 102 | shipped |
- The SELECT beforehand shows the target row. Its id identifies the order without depending on the status that is about to change.
- SET status = 'shipped' changes only that attribute. WHERE id = 102 limits the scope to that order.
- The final check shows the new status. The order's amount and customer stay as they were.
- 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
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.
The row already exists: change only status on the requested order.
Use SET status = ...
Keep WHERE id = 102.
Solution explained
UPDATE orders SET status = 'shipped' WHERE id = 102;The primary key identifies a single row and makes the scope easy to verify.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Update another order
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.
Find the row by its id.
Keep the same condition before, during and after the UPDATE.
SET status = 'shipped' changes the attribute you were asked to change.
Solution explained
SELECT id, status FROM orders WHERE id = 105; UPDATE orders SET status = 'shipped' WHERE id = 105; SELECT id, status FROM orders WHERE id = 105;Only the status of order 105 changes. Its amount and the other orders stay as they were.
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