Generate SQL to Find Your Top Selling Products by Revenue Last Month
SELECT p.product_name, SUM(oi.quantity * oi.unit_price) AS total_revenue FROM products p JOIN order_items oi ON p.product_id = oi.product_id JOIN orders o ON oi.order_id = o.order_id WHERE o.order_date >= DATE('now', '-1 month') GROUP BY p.product_name ORDER BY total_revenue DESC LIMIT 10;This SQL query is designed to pinpoint your highest-performing products based on revenue generated in the last month. It works by joining three key tables: products (to get product names), order_items (which links products to orders and contains pricing/quantity details), and orders (to filter by order date).
The WHERE clause effectively filters orders placed within the last 30 days using DATE('now', '-1 month'). The SUM(oi.quantity * oi.unit_price) calculates the total revenue for each product, and GROUP BY p.product_name aggregates this revenue per product. Finally, ORDER BY total_revenue DESC LIMIT 10 sorts the results to show your top 10 revenue-generating products.