You're managing a product sales database for a growing e-commerce company! ๐ Your goal is to analyze sales data to understand which products are performing best by calculating the total quantity sold for each product across all years.
You have two tables to work with:
Sales Table: Contains sales records with columns for sale_id, product_id, year, quantity, and price. Each row represents a single sale transaction.
Product Table: Contains product information with product_id and product_name.
Your Task: Write a SQL query that calculates the total quantity sold for every product. The result should show product_id and the sum of all quantities sold for that product across all years.
๐ก Think of this as creating a sales summary report that helps business analysts understand product performance at a glance!
Input & Output
Visualization
Time & Space Complexity
Single pass through all sales records, database handles grouping efficiently
Space for k distinct product groups, typically much smaller than total records
Constraints
- 1 โค sales table rows โค 1000
- 1 โค product table rows โค 100
- 1 โค product_id โค 100
- 2000 โค year โค 2023
- 1 โค quantity โค 104
- 1 โค price โค 106
- Primary key constraint: (sale_id, year) is unique in Sales table