List Available Prices by Product and Store

Quick Overview

List one row per product and store when a price exists, including available prices of zero.

List Available Prices by Product and Store

Company: Google

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Return product_id, store and price for each non-NULL store price in Products. The three store labels are store1, store2 and store3. Omit unavailable product-store combinations rather than emitting their NULL prices. Result row order is unrestricted. ### Tables Table: `Products` ```text +-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | store1 | int | | store2 | int | | store3 | int | +-------------+---------+ product_id is the primary key (column with unique values) for this table. Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3. If the product is not available in a store, the price will be null in that store's column. ``` ### Example ```text Products (product_id,store1,store2,store3): (8,0,NULL,20), (9,NULL,30,NULL) Output (product_id,store,price): (8,store1,0), (8,store3,20), (9,store2,30) ``` ### What a Strong Answer Covers - Return one row for each available product price and label the store correctly. - Omit NULL prices and retain a valid price of zero. ### Follow-up Questions - How would the design change if the number of stores were unknown? - How would you compare the cheapest available price for each product?

Overview: List one row per product and store when a price exists, including available prices of zero.

Google logo
Google
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return product_id, store and price for each non-NULL store price in Products. The three store labels are store1, store2 and store3. Omit unavailable product-store combinations rather than emitting their NULL prices. Result row order is unrestricted.

Tables

Table: Products

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_id  | int     |
| store1      | int     |
| store2      | int     |
| store3      | int     |
+-------------+---------+
product_id is the primary key (column with unique values) for this table.
Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3.
If the product is not available in a store, the price will be null in that store's column.

Example

Products (product_id,store1,store2,store3): (8,0,NULL,20), (9,NULL,30,NULL)
Output (product_id,store,price): (8,store1,0), (8,store3,20), (9,store2,30)

What a Strong Answer Covers Guidance

  • Return one row for each available product price and label the store correctly.
  • Omit NULL prices and retain a valid price of zero.

Follow-up Questions Guidance

  • How would the design change if the number of stores were unknown?
  • How would you compare the cheapest available price for each product?