Functions, Dates and Cleaning · Problem 17 of 118

Twenty Percent Off

EasyFunctions, Dates and CleaningShop data

A sale gives 20% off. For products with a price of 40 or more, return product_id, price and sale_price (the price after the discount, rounded to 2 decimals), sorted by product_id.

Hint 1

20% off means multiplying by 0.8.

Hint 2

ROUND(value, 2) keeps two decimals.

Reference solution

SELECT product_id,
       price,
       ROUND(price * 0.8, 2) AS sale_price
FROM products
WHERE price >= 40
ORDER BY product_id;

"20% off" is price * (1 - 0.20), which is price * 0.8. Floating-point arithmetic can leave tiny errors (86.9 * 0.8 gives 69.52000000000001 in most programming languages), so money results are rounded explicitly. For exact money, store whole cents as integers (the Bank data does).