Brightlane's pricing team is building a bulk-discount model and needs to simulate adjusted order totals.
Write a query to return each order's ID and adjusted_total:
- For orders with
total_amount > $800, the adjusted total is the originaltotal_amountscaled by0.9(a 10% reduction). - For all other orders, the adjusted total equals the original
total_amount(unchanged).
Assumptions:
- The
orderstable contains every order Brightlane has processed. - An order priced exactly at
$800is unchanged (threshold is strictly greater-than).
Output:
- One row per order, with columns
idandadjusted_total.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
CASE
WHEN total_amount > 800 THEN total_amount * 0.9
ELSE total_amount
END AS adjusted_total
FROM
orders The shape
The THEN branches return numbers, not labels — one branch computes the discounted total, the other returns the original total unchanged. CASE works the same way for numeric outputs as it does for string outputs: each branch just needs to return a value of a compatible type.
Clause by clause
SELECT idcarries the order ID through so the adjusted total has an identifier next to it.CASE WHEN total_amount > 800 THEN total_amount * 0.9 ELSE total_amount END AS adjusted_totalis the derived column. TheWHENtests each order'stotal_amountagainst the $800 threshold (strictly greater-than, so an order at exactly $800 falls through toELSEand is unchanged — matching the prompt directly).THEN total_amount * 0.9is the discounted branch. The expression references the same column the condition just tested —CASElets each branch read any column from the current row. Multiplying by0.9is the inline form of a 10% reduction (same logic as the parentheses problem in N001:* 0.9is one operation;total_amount - (total_amount * 0.1)is two).ELSE total_amountreturns the original value unchanged. This is the load-bearing detail — it makes the column complete for every order rather thanNULLfor orders below the threshold.END AS adjusted_totalcloses the expression and labels the column.FROM ordersis the source set: every order, with no filtering.
Why this and not omit the ELSE
Dropping ELSE total_amount would leave orders at or below $800 with NULL in adjusted_total. The pricing model would then have to handle missing values throughout — checking for NULL everywhere downstream, deciding whether to treat them as zero or as the original total. Putting ELSE total_amount in the CASE makes the unchanged path explicit at the source. The branch returns a real number rather than the absence of one, and every order's adjusted total reads as a single coherent column.
You practiced using CASE to produce a derived numeric value rather than a label string. The recurring shape: each branch returns whatever expression you need — strings, numbers, calculations — as long as all branches resolve to a compatible type.