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
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
Output Format:
A normalized table with columns
Example:
Input table with wide format โ Output table with long format (one row per product-store combination)
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
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
โ Linear Growth
Space Complexity
O(k)
Where k is the number of non-NULL price entries (typically much less than nรs)
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code