Calculating total
- SQL course
- Beginner
- Lesson 45/115
- 5 min
Write the query that computes the cart total.
Updating quantity
To change a cart line's quantity you use UPDATE. WHERE picks which row to update.
UPDATE cart
SET qty = 3
WHERE id = 1;This query sets the quantity of line 1 to 3.
In cart, qty = 3 is updated. Only the rows where id = 1 holds are affected.
Updating quantity — details
Changing quantity is the most common cart operation. Without WHERE every row updates; be careful.
Practice Updating quantity
Set qty to 3 for the line with id=1 in cart.
Starter code
-- update quantity
UPDATE cart
SET
WHERE id = 1Sample solution
UPDATE cart
SET qty = 3
WHERE id = 1Expected output
1 satır güncellendi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Calculating total: match the terms
| Term | Meaning |
|---|---|
UPDATE |
update |
SET |
new value |
WHERE |
condition |
INSERT |
insert |
2 Which statement updates the quantity?
- INSERT
- DELETE
- SELECT
- UPDATE Correct answer
Explanation: UPDATE changes the quantity.
3 New quantity
UPDATE cart
____ qty = 3
WHERE id = 1;
UPDATE cart
SET qty = 3
WHERE id = 1;
Explanation: The quantity is updated with UPDATE cart SET qty = 3; WHERE picks the row.