Brightlane's inventory team is flagging high-exposure products — items where the listed price per unit currently in stock exceeds $50.
Write a query to return the name and price-per-unit-in-stock of every qualifying product.
Assumptions:
- A product's price-per-unit-in-stock is its
pricedivided by itsstock_qty. - Some products have a
stock_qtyof0; those products do not have a defined price-per-unit-in-stock and should not appear in the report. - Only products whose price-per-unit-in-stock exceeds
$50should appear.
Output:
- One row per qualifying product, with columns
nameandprice_per_unit.
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
name,
price / NULLIF(stock_qty, 0) AS price_per_unit
FROM
products
WHERE
price / NULLIF(stock_qty, 0) > 50 The shape
NULLIF(stock_qty, 0) makes the division safe in both the SELECT list and the WHERE clause, and NULL's behavior in a comparison handles the rest. A sold-out product yields a NULL price / NULLIF(stock_qty, 0), and NULL > 50 is not true, so the row drops out of the filter on its own. No separate stock_qty <> 0 check is needed.
Clause by clause
SELECT name, price / NULLIF(stock_qty, 0) AS price_per_unitreturns the name and per-unit price for each row that survives the filter. TheNULLIFswaps a zero stock value for NULL before the division, so the expression yields a real number for in-stock products and NULL for sold-out ones.FROM productsreads the catalog. Sold-out products will be filtered out by theWHEREclause downstream.WHERE price / NULLIF(stock_qty, 0) > 50re-evaluates the same guarded division and keeps only the rows where the result exceeds50. For a sold-out product, the division returns NULL, andNULL > 50returns NULL, whichWHEREtreats as not-true. Those rows are silently dropped, which is exactly the behavior the report needs.
Why repeat the expression in WHERE and not just WHERE price / stock_qty > 50
The unguarded division in the WHERE would raise a division-by-zero error the moment PostgreSQL reaches a sold-out product, and the whole query would fail. Wrapping stock_qty in NULLIF(stock_qty, 0) in both places keeps the divisor non-zero everywhere it appears. The duplication reads as repetition but is load-bearing: each occurrence of the division needs its own guard.
The trap
The filter looks like it might also keep sold-out rows with price_per_unit showing as NULL, since the SELECT list produces NULL for them. It does not. WHERE keeps a row only when its condition evaluates to true; NULL is not true. A WHERE predicate that returns NULL drops the row exactly as if it had returned false. This is the same NULL-vs-three-valued-logic rule that governs every WHERE comparison, and knowing it means an explicit WHERE stock_qty <> 0 is unnecessary because the NULL produced by NULLIF already does that work.
You practiced combining a NULLIF-guarded division with a threshold check — undefined calculations evaluate to missing and drop out of the comparison cleanly.