This problem requires querying two tables, Sales
and Product
, to determine the total quantity sold for each product. The solution doesn't require joining the tables because the question only asks for the total quantity per product ID, which is available solely within the Sales
table.
The core idea is to use SQL's aggregate function SUM()
along with GROUP BY
to achieve the desired result.
SELECT product_id, SUM(quantity) AS total_quantity
: This selects the product_id
and calculates the sum of the quantity
column, aliasing the sum as total_quantity
.
FROM Sales
: This specifies that the data should be retrieved from the Sales
table.
GROUP BY 1
: This groups the results by the first column in the SELECT
statement, which is product_id
. This ensures that the SUM()
function aggregates quantities for each unique product ID. Using 1
as shorthand for the first column is a common SQL optimization.
SELECT product_id, SUM(quantity) AS total_quantity
FROM Sales
GROUP BY 1;
Time Complexity: O(N log N), where N is the number of rows in the Sales
table. The GROUP BY
operation involves sorting or hashing (depending on the database implementation) to group rows with the same product_id
. Sorting usually has O(N log N) complexity while hashing can be O(N) in average case.
Space Complexity: O(M), where M is the number of unique product IDs. This space is used to store the intermediate results during the GROUP BY
operation. In the worst case, M could be equal to N (if all product IDs are unique), but in practice, it would usually be smaller.
While the provided MySQL solution is highly efficient and directly addresses the problem statement, we could theoretically extend it to include the product name if the problem were to request it:
SELECT p.product_name, SUM(s.quantity) AS total_quantity
FROM Sales s
JOIN Product p ON s.product_id = p.product_id
GROUP BY p.product_name;
This would involve joining the Sales
and Product
tables, and then grouping the results by product_name
. However, this is unnecessary for the original problem description. The time complexity would increase slightly due to the join operation.