Sponsored
Sponsored
We will use an SQL query with an INNER JOIN to combine the Sales and Product tables based on the product_id. INNER JOIN will ensure we fetch records that have corresponding IDs in both tables. After joining, we will project the columns product_name, year, and price.
Time Complexity: O(N) where N is the number of rows in Sales table.
Space Complexity: O(1) as we use a fixed amount of extra space for the query execution.
1SELECT P.product_name, S.year, S.price FROM Sales S INNER JOIN Product P ON S.product_id = P.product_id;
This SQL query selects the product_name from the Product table and year and price from the Sales table. We use INNER JOIN
to join these two tables based on product_id
.
This approach involves using programming constructs to emulate a join operation typically done in SQL. We'll utilize hash maps (or dictionaries) in different languages to store data from one table and then iterate through the other to join and construct the result.
Time Complexity: O(N + M) where N is the number of products and M is the number of sales.
Space Complexity: O(N) to store the dictionary mapping product_id to product_name.
1def product_sales_analysis(sales, products):
2 product_dict
We create a dictionary mapping product_id
to product_name
. Then, we iterate through the Sales table, using the mapped dictionary to find the relevant product name and appending this data to a result list.