This problem requires querying a database table (Products
) to find the price of each product in each store. The table's structure includes product_id
, store
(an enum with values 'store1', 'store2', 'store3'), and price
. The goal is to create a result table showing each product's price for each store, handling cases where a product isn't available in a particular store (represented by NULL).
The most efficient approach uses SQL's aggregate functions and conditional logic to pivot the data. We group the data by product_id
and use conditional aggregation (e.g., SUM(IF(...))
in MySQL) to extract the price for each store.
SELECT
product_id,
SUM(IF(store = 'store1', price, NULL)) AS store1,
SUM(IF(store = 'store2', price, NULL)) AS store2,
SUM(IF(store = 'store3', price, NULL)) AS store3
FROM Products
GROUP BY 1;
Explanation:
SELECT product_id
: This selects the product ID, which will be the primary key in our result table.
SUM(IF(store = 'store1', price, NULL)) AS store1
: This is the core logic. IF(store = 'store1', price, NULL)
checks if the store
column is 'store1'. If true, it returns the price
; otherwise, it returns NULL
. The SUM()
function then aggregates these values. If a product has a price in 'store1', the SUM()
will return that price; otherwise, it will return NULL
. We repeat this for 'store2' and 'store3'.
FROM Products
: This specifies the table to query.
GROUP BY 1
: This groups the results by the first column in the SELECT
statement (which is product_id
). This ensures that we get one row per product.
The time complexity of this SQL query is dominated by the GROUP BY
operation. In the worst-case scenario (where the Products
table has a large number of rows), the time complexity would be O(N log N) or O(N) depending on the specific database implementation and indexing. However, for reasonably sized datasets, the query will be very fast. The conditional aggregation within each SUM()
operation adds a constant time factor to each row processing, but it doesn't change the overall complexity order.
The space complexity depends on the size of the resulting table. In the worst-case scenario, the space required will be proportional to the number of unique product_id
values. So the space complexity can be considered O(M), where M is the number of unique products. This space is used to store the intermediate results and the final output.