Build a store product catalog view with stock status and discount percentage

Quick 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.

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.

Instacart logo
Instacart
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

Input Table(s)

stores

ColumnTypeMeaning
store_idINTStore identifier
store_nameVARCHAR(255)Store name
banner_nameVARCHAR(255)Retail banner (chain brand) the store trades under

Primary key: store_id.

products

ColumnTypeMeaning
product_idINTProduct identifier
product_nameVARCHAR(255)Product name
categoryVARCHAR(100)Product category
brandVARCHAR(100)Brand name

Primary key: product_id.

store_products

ColumnTypeMeaning
store_idINTStore that lists the product
product_idINTListed product
priceDECIMAL(10,2)Regular price at this store
discount_priceDECIMAL(10,2)Sale price at this store, NULL when there is no sale
is_activeINT1 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

ColumnTypeMeaning
store_idINTStore
product_idINTProduct
quantity_on_handINTUnits currently in stock, NOT NULL
reorder_levelINTStock 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_idstore_namebanner_name
1Downtown MarketFreshCo
2LakesideValueMart

products

product_idproduct_namecategorybrand
101Whole Milk 1 galDairyHillside
102Sourdough LoafBakeryStone Oven
103Bananas 1 lbProduceSunvale

store_products

store_idproduct_idpricediscount_priceis_active
11014.993.991
11026.50NULL1
11030.690.791
21015.29NULL0
21026.004.501

store_inventory

store_idproduct_idquantity_on_handreorder_level
1101010
11021212
110314050
21013010

Expected result:

store_idstore_namebanner_nameproduct_idproduct_namecategorybrandpricediscount_pricequantity_on_handreorder_levelStockStatusDiscountPct
1Downtown MarketFreshCo101Whole Milk 1 galDairyHillside4.993.99010Out of Stock20.04
1Downtown MarketFreshCo102Sourdough LoafBakeryStone Oven6.50NULL1212Low Stock0.00
1Downtown MarketFreshCo103Bananas 1 lbProduceSunvale0.690.7914050In Stock0.00
2LakesideValueMart102Sourdough LoafBakeryStone Oven6.004.50NULLNULLIn Stock25.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.