MediumFunctions, Dates and CleaningShop dataSolved
The margin of a product is (price - cost) / price, shown as a percentage with one decimal.
Return name and margin_percent for the five products with the highest margin. Sort by margin_percent from high to low, then by product_id.
Hint 1
Multiply by 100.0 to get a percentage.
Hint 2
You can sort by the expression itself, or by its alias.
Reference solution
SELECT name,
ROUND(100.0 * (price - cost) / price, 1) AS margin_percent
FROM products
ORDER BY margin_percent DESC, product_id
LIMIT 5;
Margin divides the profit by the selling price; markup divides it by the cost. They are different numbers and people mix them up in real meetings. You can use a column alias (margin_percent) in ORDER BY, but not in WHERE (the filter runs before the select list is computed).