By quantity sold from delivered orders
Data table
SQL query
SELECT p.name AS product_name,
COALESCE(SUM(oi.quantity), 0) AS quantity_sold
FROM products p
JOIN order_items oi ON oi.product_id = p.id
JOIN orders o ON o.id = oi.order_id
WHERE o.status = 'delivered'
AND o.order_date >= DATE :start_date
AND o.order_date < DATE :end_date + INTERVAL '1 day'
GROUP BY p.id, p.name
ORDER BY quantity_sold DESC
LIMIT 5;
Revenue by product category for delivered orders
Data table
SQL query
SELECT cat.name AS category,
COALESCE(SUM(oi.quantity * oi.unit_price), 0) AS revenue
FROM categories cat
JOIN products p ON p.category_id = cat.id
JOIN order_items oi ON oi.product_id = p.id
JOIN orders o ON o.id = oi.order_id
WHERE o.status = 'delivered'
AND o.order_date >= DATE :start_date
AND o.order_date < DATE :end_date + INTERVAL '1 day'
GROUP BY cat.id, cat.name
ORDER BY revenue DESC;