UPDATESQL

Unit 2, lesson 6 · COALESCE

Missing values in calculations: COALESCE

Work out the sale price

Any arithmetic with NULL gives NULL: 100 * (1 - NULL) is NULL, not 100. COALESCE(value, fallback) returns the first value that is not NULL, so COALESCE(discount, 0) turns a missing discount into 0 and leaves real discounts alone. Use it on a column before you calculate with it.

Example
SELECT name,
       discount,
       COALESCE(discount, 0) AS discount_or_zero
FROM products;

Your task

Show every product's name, price and sale_price, where sale_price is the price after its discount, rounded to cents. Products with no discount sell at full price.

Tables you will use: products

Try it in your browserFree, no sign-up. Your query is checked as you go.