Sequence NODE_299
MediumFind Top N Products by Sales
SQL
Technical Specification
Using order_items and products, find top 5 products by total revenue (quantity * price).
Input/Output Samples
Input:order_items + products
Output:Top 5 products
Optimal Logic Path
SELECT
p.id,
p.name,
SUM(oi.quantity * oi.price) AS total_revenue
FROM order_items oi
JOIN products p ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY total_revenue DESC
LIMIT 5;Architectural Deep-Dive
Multiplying quantity and price in aggregation reveals top-earning products.