Product's Price for Each Store - Problem

๐Ÿช Product Price Comparison Across Stores

Imagine you're building a price comparison feature for an e-commerce platform! You have a database table containing product prices across different stores, but the data is stored in a normalized format (each row represents one product-store combination).

Your task: Transform this data into a user-friendly format where each product shows its price in all stores side by side, making it easy for customers to compare prices at a glance.

๐Ÿ“Š Database Schema

Table: Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store | enum |
| price | int |
+-------------+---------+
Primary Key: (product_id, store)
Store values: 'store1', 'store2', 'store3'

Goal: Create a result table where each row represents a product, and columns show the price in each store (store1, store2, store3). If a product isn't available in a store, show null.

This is a classic pivot table operation - converting rows to columns for better data presentation!

Input & Output

example_1.sql โ€” Basic Pivot Operation
$ Input: Products table: +------------+--------+-------+ | product_id | store | price | +------------+--------+-------+ | 0 | store1 | 95 | | 0 | store3 | 105 | | 0 | store2 | 100 | | 1 | store1 | 70 | | 1 | store2 | 80 | +------------+--------+-------+
โ€บ Output: +------------+--------+--------+--------+ | product_id | store1 | store2 | store3 | +------------+--------+--------+--------+ | 0 | 95 | 100 | 105 | | 1 | 70 | 80 | null | +------------+--------+--------+--------+
๐Ÿ’ก Note: Product 0 is available in all three stores with prices 95, 100, and 105 respectively. Product 1 is only available in store1 (70) and store2 (80), so store3 shows null.
example_2.sql โ€” Single Product Multiple Stores
$ Input: Products table: +------------+--------+-------+ | product_id | store | price | +------------+--------+-------+ | 5 | store1 | 200 | | 5 | store2 | 180 | | 5 | store3 | 220 | +------------+--------+-------+
โ€บ Output: +------------+--------+--------+--------+ | product_id | store1 | store2 | store3 | +------------+--------+--------+--------+ | 5 | 200 | 180 | 220 | +------------+--------+--------+--------+
๐Ÿ’ก Note: Product 5 is available in all three stores, showing how a single product's prices get pivoted into one row with separate columns for each store.
example_3.sql โ€” Edge Case: Products with Limited Availability
$ Input: Products table: +------------+--------+-------+ | product_id | store | price | +------------+--------+-------+ | 10 | store2 | 50 | | 11 | store3 | 75 | | 12 | store1 | 25 | +------------+--------+-------+
โ€บ Output: +------------+--------+--------+--------+ | product_id | store1 | store2 | store3 | +------------+--------+--------+--------+ | 10 | null | 50 | null | | 11 | null | null | 75 | | 12 | 25 | null | null | +------------+--------+--------+--------+
๐Ÿ’ก Note: Each product is available in only one store, demonstrating how the pivot operation handles sparse data by filling unavailable combinations with null values.

Visualization

Tap to expand
Input: Normalized Dataproduct_id | store | price1 | store1 | 951 | store2 | 1002 | store1 | 70Output: Pivoted Dataproduct_id | store1 | store21 | 95 | 1002 | 70 | nullMagic: CASE + GROUP BYMAX(CASE WHEN store='store1'THEN price END) AS store1GROUP BY product_idSingle PassO(n) Time
Understanding the Visualization
1
Identify the Pattern
We have product-store-price combinations that need to become product rows with store columns
2
Group by Product
Collect all rows for each product ID together
3
Apply Conditional Logic
Use CASE statements to route each price to the correct store column
4
Aggregate the Results
Use MAX to extract the single price value for each store (or NULL if not available)
5
Generate Final Table
Output one row per product with separate columns for each store's price
Key Takeaway
๐ŸŽฏ Key Insight: Conditional aggregation allows us to transform rows into columns efficiently by using CASE statements within aggregate functions, all while maintaining optimal O(n) performance with a single table scan.

Time & Space Complexity

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

Single pass through all rows in the products table

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

Where k is the number of unique products (for GROUP BY operation)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of rows โ‰ค 104
  • store is an ENUM of type ('store1', 'store2', 'store3')
  • 1 โ‰ค product_id โ‰ค 106
  • 1 โ‰ค price โ‰ค 106
  • Primary key: (product_id, store) - no duplicate combinations
Asked in
Amazon 45 Google 35 Microsoft 30 Meta 25
32.0K Views
High Frequency
~15 min Avg. Time
1.5K 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