Build a store product catalog view with stock status and discount percentage
Company: Instacart
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A multi-store grocery catalog is kept in four normalized tables: stores, products, the store-product listings that carry each store's price and active flag, and per-listing inventory. Write a query that produces a catalog view with one row for every active store-product pair (`is_active = 1`). Each row shows the store's details, the product's details, the listing's pricing and inventory, and two computed columns, `StockStatus` and `DiscountPct`.
```hint Listings without inventory
Work out which CASE branch a listing with no inventory row falls into, and make sure your joins keep that listing in the result.
```
### Input Table(s)
`stores`
| Column | Type | Meaning |
|---|---|---|
| store_id | INT | Store identifier |
| store_name | VARCHAR(255) | Store name |
| banner_name | VARCHAR(255) | Retail banner (chain brand) the store trades under |
Primary key: `store_id`.
`products`
| Column | Type | Meaning |
|---|---|---|
| product_id | INT | Product identifier |
| product_name | VARCHAR(255) | Product name |
| category | VARCHAR(100) | Product category |
| brand | VARCHAR(100) | Brand name |
Primary key: `product_id`.
`store_products`
| Column | Type | Meaning |
|---|---|---|
| store_id | INT | Store that lists the product |
| product_id | INT | Listed product |
| price | DECIMAL(10,2) | Regular price at this store |
| discount_price | DECIMAL(10,2) | Sale price at this store, NULL when there is no sale |
| is_active | INT | 1 when the listing is active, 0 otherwise |
Primary key: (`store_id`, `product_id`). `store_id` references `stores.store_id` and `product_id` references `products.product_id`.
`store_inventory`
| Column | Type | Meaning |
|---|---|---|
| store_id | INT | Store |
| product_id | INT | Product |
| quantity_on_hand | INT | Units currently in stock, NOT NULL |
| reorder_level | INT | Stock threshold for reordering, NOT NULL |
Primary key: (`store_id`, `product_id`), referencing `store_products`. A listing may have no row in this table.
### Output Contract
One row per `store_products` row with `is_active = 1`, with these columns in this order:
`store_id`, `store_name`, `banner_name`, `product_id`, `product_name`, `category`, `brand`, `price`, `discount_price`, `quantity_on_hand`, `reorder_level`, `StockStatus`, `DiscountPct`
- `StockStatus` is `'Out of Stock'` when `quantity_on_hand` is 0, `'Low Stock'` when `quantity_on_hand` is positive and at most `reorder_level`, and `'In Stock'` otherwise. "Otherwise" includes listings with no inventory row, whose quantity is NULL.
- `DiscountPct` is `100 * (price - discount_price) / price` rounded to 2 decimal places when `discount_price` is not NULL and is less than `price`, and `0.00` otherwise.
- The two computed column names are case-sensitive and must be exactly `StockStatus` and `DiscountPct`.
- Sort by `store_id` ascending, then `product_id` ascending.
### Example
`stores`
| store_id | store_name | banner_name |
|---|---|---|
| 1 | Downtown Market | FreshCo |
| 2 | Lakeside | ValueMart |
`products`
| product_id | product_name | category | brand |
|---|---|---|---|
| 101 | Whole Milk 1 gal | Dairy | Hillside |
| 102 | Sourdough Loaf | Bakery | Stone Oven |
| 103 | Bananas 1 lb | Produce | Sunvale |
`store_products`
| store_id | product_id | price | discount_price | is_active |
|---|---|---|---|---|
| 1 | 101 | 4.99 | 3.99 | 1 |
| 1 | 102 | 6.50 | NULL | 1 |
| 1 | 103 | 0.69 | 0.79 | 1 |
| 2 | 101 | 5.29 | NULL | 0 |
| 2 | 102 | 6.00 | 4.50 | 1 |
`store_inventory`
| store_id | product_id | quantity_on_hand | reorder_level |
|---|---|---|---|
| 1 | 101 | 0 | 10 |
| 1 | 102 | 12 | 12 |
| 1 | 103 | 140 | 50 |
| 2 | 101 | 30 | 10 |
Expected result:
| store_id | store_name | banner_name | product_id | product_name | category | brand | price | discount_price | quantity_on_hand | reorder_level | StockStatus | DiscountPct |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Downtown Market | FreshCo | 101 | Whole Milk 1 gal | Dairy | Hillside | 4.99 | 3.99 | 0 | 10 | Out of Stock | 20.04 |
| 1 | Downtown Market | FreshCo | 102 | Sourdough Loaf | Bakery | Stone Oven | 6.50 | NULL | 12 | 12 | Low Stock | 0.00 |
| 1 | Downtown Market | FreshCo | 103 | Bananas 1 lb | Produce | Sunvale | 0.69 | 0.79 | 140 | 50 | In Stock | 0.00 |
| 2 | Lakeside | ValueMart | 102 | Sourdough Loaf | Bakery | Stone Oven | 6.00 | 4.50 | NULL | NULL | In Stock | 25.00 |
Listing (2, 101) is inactive, so it is left out even though it has inventory. Listing (1, 102) sits exactly at its reorder level, which counts as low stock. Listing (1, 103) has a sale price above the regular price, so its discount is 0.00. Listing (2, 102) has no inventory row: its inventory columns are NULL, it falls through to `'In Stock'`, and its discount is still computed as `100 * 1.50 / 6.00 = 25.00`.
### Constraints and Clarifications
- `stores` has at most 10,000 rows, `products` at most 1,000,000, and `store_products` at most 10,000,000.
- `price` is positive. `discount_price` is NULL or positive and can be equal to or above `price`, in which case `DiscountPct` is `0.00`.
- `quantity_on_hand >= 0` and `reorder_level >= 0`, and neither is NULL when an inventory row exists.
- `is_active` is 0 or 1. Inactive listings are excluded whether or not they have inventory.
- Each listing has at most one inventory row, so every active listing produces exactly one output row.
- For a listing with no inventory row, `quantity_on_hand` and `reorder_level` are NULL in the output, `StockStatus` is `'In Stock'` by the rule above, and `DiscountPct` is computed as usual.
Overview: Write a SQL query that builds a store-level product catalog from four normalized tables, keeping only active listings and adding a stock status label and a rounded discount percentage. It tests outer joins, CASE logic when inventory rows are missing, and exact percentage math in PostgreSQL.