Changing Data · Problem 109 of 118

Raise Prices in One Category

EasyChanging DataShop data

Raise the price of every product in category 2 by 10%, rounded to 2 decimals. Products in other categories must not change.

Check query. After your statements run, this query is run on the changed data and its result is compared with the expected one:

SELECT category_id, ROUND(SUM(price), 2) AS total_price
FROM products
GROUP BY category_id
ORDER BY category_id;
Hint 1

UPDATE products SET price = ... WHERE .... The new price is computed from the old price in the same statement.

Reference solution

UPDATE products
SET price = ROUND(price * 1.1, 2)
WHERE category_id = 2;

UPDATE without a WHERE changes every row. That is the most expensive typo in SQL. The professional habit: write the WHERE first, run it as a SELECT to see which rows it picks, and only then turn it into an UPDATE. The right-hand side of SET sees the old values of the row, so price * 1.1 uses the price before the change.