GAZAR

Principal Engineer | Mentor

Calculating Total Sales by Product Category

Calculating Total Sales by Product Category

To calculate total sales by product category, we need to join the "orders" and "order_details" tables and aggregate the sales amount for each category. Here's how we can achieve this with a SQL query:

SELECT 
    p.category_name AS product_category,
    SUM(od.unit_price * od.quantity) AS total_sales
FROM 
    orders o
JOIN 
    order_details od ON o.order_id = od.order_id
JOIN 
    products p ON od.product_id = p.product_id
GROUP BY 
    p.category_name;

The result would be like this:

Screenshot 2024-04-29 at 9.29.35 PM.png

In conclusion, calculating total sales by product category involves joining relevant tables in the database and aggregating sales amounts using SQL. By understanding the data structure and employing SQL queries, businesses can gain valuable insights into their sales performance, enabling informed decision-making and strategic planning.


Comments