UPDATE changes existing rows. SET says which columns get which new values, and WHERE picks the rows. If you leave out WHERE, every row is updated. Many real-world data accidents come from that missing line, so run the matching SELECT first when you are unsure.
Each question below starts from the original products table, so you can try them one at a time. After each query we show the whole table.
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Set up the practice tables
Copy this script and run it once.
CREATE TABLE products (
prod_id CHAR(10) NOT NULL,
prod_name CHAR(255) NOT NULL,
price DECIMAL(8,2) NOT NULL,
quantity INT,
PRIMARY KEY (prod_id)
);
INSERT INTO products (prod_id, prod_name, price, quantity) VALUES
('BR01', '8 inch teddy bear', 5.99, 8),
('BR02', '12 inch teddy bear', 8.99, 12),
('BR03', '18 inch teddy bear', 11.99, 18),
('BNBG01', 'Fish bean bag toy', 3.49, 7),
('BNBG02', 'Bird bean bag toy', 3.49, 20),
('BNBG03', 'Rabbit bean bag toy', 3.49, 9),
('RGAN01', 'Raggedy Ann', 4.99, 15),
('RYL01', 'King doll', 9.49, 12),
('RYL02', 'Queen doll', 9.49, 26);Table: products
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.49 | 7 |
| BNBG02 | Bird bean bag toy | 3.49 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.49 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 11.99 | 18 |
| RGAN01 | Raggedy Ann | 4.99 | 15 |
| RYL01 | King doll | 9.49 | 12 |
| RYL02 | Queen doll | 9.49 | 26 |
You can do these in any order. Each answer below was run on a fresh copy of the original table, so earlier queries do not change later results.
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Increase the price of all products by 10%.
Show the answer and the output
UPDATE products SET price = price * 1.1;Output
Query OK, 9 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.84 | 7 |
| BNBG02 | Bird bean bag toy | 3.84 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.84 | 9 |
| BR01 | 8 inch teddy bear | 6.59 | 8 |
| BR02 | 12 inch teddy bear | 9.89 | 12 |
| BR03 | 18 inch teddy bear | 13.19 | 18 |
| RGAN01 | Raggedy Ann | 5.49 | 15 |
| RYL01 | King doll | 10.44 | 12 |
| RYL02 | Queen doll | 10.44 | 26 |
Question 2Set the price of all products with a quantity less than 10 to 5.99.
Show the answer and the output
UPDATE products SET price = 5.99 WHERE quantity < 10;Output
Query OK, 2 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 5.99 | 7 |
| BNBG02 | Bird bean bag toy | 3.49 | 20 |
| BNBG03 | Rabbit bean bag toy | 5.99 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 11.99 | 18 |
| RGAN01 | Raggedy Ann | 4.99 | 15 |
| RYL01 | King doll | 9.49 | 12 |
| RYL02 | Queen doll | 9.49 | 26 |
Question 3Double the price of products with a quantity between 15 and 50.
Show the answer and the output
UPDATE products SET price = price * 2 WHERE quantity BETWEEN 15 AND 50;Output
Query OK, 4 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.49 | 7 |
| BNBG02 | Bird bean bag toy | 6.98 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.49 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 23.98 | 18 |
| RGAN01 | Raggedy Ann | 9.98 | 15 |
| RYL01 | King doll | 9.49 | 12 |
| RYL02 | Queen doll | 18.98 | 26 |
Question 4Update the product name of the product with prod_id 'RGAN01' to 'Barbie Doll' and also update its price to 6.99.
Show the answer and the output
UPDATE products
SET prod_name = 'Barbie Doll', price = 6.99
WHERE prod_id = 'RGAN01';Output
Query OK, 1 row affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.49 | 7 |
| BNBG02 | Bird bean bag toy | 3.49 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.49 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 11.99 | 18 |
| RGAN01 | Barbie Doll | 6.99 | 15 |
| RYL01 | King doll | 9.49 | 12 |
| RYL02 | Queen doll | 9.49 | 26 |
Question 5Increase the price by 5% for products with the word 'toy' in the name.
Show the answer and the output
UPDATE products SET price = price * 1.05 WHERE prod_name LIKE '%toy%';Output
Query OK, 3 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.66 | 7 |
| BNBG02 | Bird bean bag toy | 3.66 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.66 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 11.99 | 18 |
| RGAN01 | Raggedy Ann | 4.99 | 15 |
| RYL01 | King doll | 9.49 | 12 |
| RYL02 | Queen doll | 9.49 | 26 |
Question 6Add the prefix 'SALE: ' to the beginning of the product name for all products.
Show the answer and the output
UPDATE products SET prod_name = CONCAT('SALE: ', prod_name);Output
Query OK, 9 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | SALE: Fish bean bag toy | 3.49 | 7 |
| BNBG02 | SALE: Bird bean bag toy | 3.49 | 20 |
| BNBG03 | SALE: Rabbit bean bag toy | 3.49 | 9 |
| BR01 | SALE: 8 inch teddy bear | 5.99 | 8 |
| BR02 | SALE: 12 inch teddy bear | 8.99 | 12 |
| BR03 | SALE: 18 inch teddy bear | 11.99 | 18 |
| RGAN01 | SALE: Raggedy Ann | 4.99 | 15 |
| RYL01 | SALE: King doll | 9.49 | 12 |
| RYL02 | SALE: Queen doll | 9.49 | 26 |
Question 7Decrease the stock quantity by 1 for products with a quantity more than 10.
Show the answer and the output
UPDATE products SET quantity = quantity - 1 WHERE quantity > 10;Output
Query OK, 6 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.49 | 7 |
| BNBG02 | Bird bean bag toy | 3.49 | 19 |
| BNBG03 | Rabbit bean bag toy | 3.49 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 11 |
| BR03 | 18 inch teddy bear | 11.99 | 17 |
| RGAN01 | Raggedy Ann | 4.99 | 14 |
| RYL01 | King doll | 9.49 | 11 |
| RYL02 | Queen doll | 9.49 | 25 |
Question 8Update the quantity to NULL for products with a price more than 9.
Show the answer and the output
UPDATE products SET quantity = NULL WHERE price > 9;Output
Query OK, 3 rows affected.
Then check with SELECT * FROM products ORDER BY prod_id
| prod_id | prod_name | price | quantity |
|---|---|---|---|
| BNBG01 | Fish bean bag toy | 3.49 | 7 |
| BNBG02 | Bird bean bag toy | 3.49 | 20 |
| BNBG03 | Rabbit bean bag toy | 3.49 | 9 |
| BR01 | 8 inch teddy bear | 5.99 | 8 |
| BR02 | 12 inch teddy bear | 8.99 | 12 |
| BR03 | 18 inch teddy bear | 11.99 | NULL |
| RGAN01 | Raggedy Ann | 4.99 | 15 |
| RYL01 | King doll | 9.49 | NULL |
| RYL02 | Queen doll | 9.49 | NULL |
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
