Product Sales Analysis II - Problem

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

example_1.sql โ€” Basic Sales Data
$ Input: Sales table: | sale_id | product_id | year | quantity | price | |---------|------------|------|----------|-------| | 1 | 100 | 2008 | 10 | 5000 | | 2 | 100 | 2009 | 12 | 5000 | | 7 | 200 | 2011 | 15 | 9000 | Product table: | product_id | product_name | |------------|-------------| | 100 | Nokia | | 200 | Apple |
โ€บ Output: | product_id | total_quantity | |------------|----------------| | 100 | 22 | | 200 | 15 |
๐Ÿ’ก Note: Product 100 (Nokia) sold 10 units in 2008 + 12 units in 2009 = 22 total units. Product 200 (Apple) sold 15 units in 2011.
example_2.sql โ€” Multiple Years Same Product
$ Input: Sales table: | sale_id | product_id | year | quantity | price | |---------|------------|------|----------|-------| | 1 | 300 | 2020 | 5 | 1000 | | 2 | 300 | 2021 | 8 | 1000 | | 3 | 300 | 2022 | 12 | 1200 | | 4 | 400 | 2021 | 3 | 800 |
โ€บ Output: | product_id | total_quantity | |------------|----------------| | 300 | 25 | | 400 | 3 |
๐Ÿ’ก Note: Product 300 has sales across three years: 5 + 8 + 12 = 25 total units. Product 400 has only one sale of 3 units.
example_3.sql โ€” Single Product Multiple Sales
$ Input: Sales table: | sale_id | product_id | year | quantity | price | |---------|------------|------|----------|-------| | 1 | 500 | 2023 | 1 | 100 | | 2 | 500 | 2023 | 1 | 100 | | 3 | 500 | 2023 | 1 | 100 |
โ€บ Output: | product_id | total_quantity | |------------|----------------| | 500 | 3 |
๐Ÿ’ก Note: Product 500 has three separate sales in the same year, each for 1 unit, totaling 3 units.

Visualization

Tap to expand
Sales Data Aggregation Visualization๐Ÿ“Š Sales TableProduct 100: Qty 10Product 200: Qty 5Product 100: Qty 15Product 200: Qty 8Product 100: Qty 5๐Ÿ”„ GROUP BY Process๐Ÿ“ฑ Product 100 Group10 + 15 + 5 = 30๐ŸŽ Product 200 Group5 + 8 = 13โœ… Final ResultProduct 100: 30Product 200: 13๐Ÿš€ Performance Benefitsโ€ข Single table scan - O(n) timeโ€ข Database-optimized aggregationโ€ข Scalable for large datasets
Understanding the Visualization
1
Scan Sales Records
Go through each sales record in the database table
2
Group by Product
Organize records into groups based on product_id
3
Sum Quantities
Add up all quantity values within each product group
4
Return Results
Output product_id and total_quantity for each group
Key Takeaway
๐ŸŽฏ Key Insight: GROUP BY with SUM() is the most efficient way to aggregate data in SQL, leveraging database optimizations for superior performance compared to manual approaches.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all sales records, database handles grouping efficiently

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for k distinct product groups, typically much smaller than total records

n
2n
โœ“ Linear Space

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
Asked in
Amazon 45 Microsoft 35 Google 28 Meta 22
28.4K Views
High Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen