Loading…
CTEs are perfect for calculating derived values:
WITH product_metrics AS (
SELECT
name,
price,
cost,
price - cost as profit,
(price - cost) / price * 100 as margin_pct
FROM products
)
SELECT * FROM product_metrics WHERE margin_pct > 50;
Create a CTE called product_margins that calculates for each product:
Then select products where margin_pct is greater than 50, ordered by margin_pct descending.
Downloading SQL engine… (one-time)
This runs entirely in your browser and is cached for next time.