Rearrange Products Table - Problem
Data Transformation Challenge: Rearrange Products Table

You're working with an e-commerce database that tracks product prices across multiple stores. The current table structure stores all store prices in separate columns, but you need to transform it into a more flexible format for analytics.

The Challenge:
Given a Products table with columns product_id, store1, store2, and store3, transform it so that each row represents a single product-store-price combination.

Input Format:
A table where each row contains a product's prices across 3 stores. If a product isn't available in a store, the price is NULL.

Output Format:
A normalized table with columns product_id, store, and price. Skip any NULL prices - only include rows where the product is actually available in that store.

Example:
Input table with wide format โ†’ Output table with long format (one row per product-store combination)

Input & Output

example_1.sql โ€” Basic Transformation
$ Input: Products table: | product_id | store1 | store2 | store3 | |------------|--------|--------|--------| | 0 | 95 | 100 | 105 | | 1 | 70 | null | 80 |
โ€บ Output: | product_id | store | price | |------------|-----------|-------| | 0 | store1 | 95 | | 0 | store2 | 100 | | 0 | store3 | 105 | | 1 | store1 | 70 | | 1 | store3 | 80 |
๐Ÿ’ก Note: Product 0 is available in all 3 stores, so we get 3 rows. Product 1 is only available in store1 and store3 (store2 is null), so we get 2 rows. Total: 5 rows in the result.
example_2.sql โ€” All Stores Available
$ Input: Products table: | product_id | store1 | store2 | store3 | |------------|--------|--------|--------| | 0 | 10 | 20 | 30 | | 1 | 40 | 50 | 60 |
โ€บ Output: | product_id | store | price | |------------|-----------|-------| | 0 | store1 | 10 | | 0 | store2 | 20 | | 0 | store3 | 30 | | 1 | store1 | 40 | | 1 | store2 | 50 | | 1 | store3 | 60 |
๐Ÿ’ก Note: Both products are available in all stores, so we get 2 ร— 3 = 6 rows in the result. Each product gets transformed into 3 rows, one for each store.
example_3.sql โ€” Many NULL Values
$ Input: Products table: | product_id | store1 | store2 | store3 | |------------|--------|--------|--------| | 0 | null | null | 105 | | 1 | 70 | null | null | | 2 | null | 80 | null |
โ€บ Output: | product_id | store | price | |------------|-----------|-------| | 0 | store3 | 105 | | 1 | store1 | 70 | | 2 | store2 | 80 |
๐Ÿ’ก Note: Each product is only available in one store: Product 0 only in store3, Product 1 only in store1, Product 2 only in store2. This results in exactly 3 rows - one per product.

Visualization

Tap to expand
BEFORE (Wide Format)product_idstore1store2store3095100105170NULL80UNION ALLAFTER (Long Format)product_idstoreprice0store1950store21000store31051store1701store380Key Insight: UNION ALL combines 3 filtered queriesQuery 1: SELECT product_id, 'store1', store1 FROM Products WHERE store1 IS NOT NULLQuery 2: SELECT product_id, 'store2', store2 FROM Products WHERE store2 IS NOT NULLQuery 3: SELECT product_id, 'store3', store3 FROM Products WHERE store3 IS NOT NULL
Understanding the Visualization
1
Identify Source Structure
Wide table with product_id and 3 store price columns
2
Plan Target Structure
Long table with product_id, store, price columns
3
Apply UNION ALL
Combine 3 filtered queries - one per store column
4
Filter NULL Values
Each query only includes rows where that store's price exists
Key Takeaway
๐ŸŽฏ Key Insight: UNION ALL efficiently combines three targeted queries, each extracting one store's data while filtering out NULL values, transforming wide format to normalized long format in a single operation.

Time & Space Complexity

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

Single pass through the Products table, scanning it three times but with early filtering

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

Where k is the number of non-NULL price entries (typically much less than nร—s)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Number of products โ‰ค 1000
  • 0 โ‰ค price โ‰ค 1000 (when not NULL)
  • Prices can be NULL indicating product not available in that store
  • product_id is unique (primary key)
  • Result should exclude rows where price IS NULL
Asked in
Amazon 45 Google 30 Meta 25 Microsoft 20
42.0K Views
High Frequency
~15 min Avg. Time
1.9K 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