Quick Overview

Write a SQL query that subtracts each warehouse and product pair's total sales from its recorded stock, compares the result with the physical count, and returns the mismatches ordered by size. It tests aggregating before a join, keeping rows that have no sales, and sorting on an absolute value with tie-breakers.

Find inventory mismatches between expected stock after sales and the physical count

Company: Shopify

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each `inventory` row records how many units of a product a warehouse had on record (`recorded_quantity`) and how many were found in a later physical count (`actual_quantity`). Every sale between the two is logged in `sales`. For each `inventory` row, compare the expected remaining stock with `actual_quantity` and return the mismatches. For each row: - `total_sold` is the sum of `quantity_sold` for that `(warehouse_id, product_id)` pair, or `0` if the pair has no sales. - `expected_remaining = recorded_quantity - total_sold`. - `discrepancy = expected_remaining - actual_quantity`. Return only rows with a non-zero `discrepancy`. ### Input Table(s) `warehouses` | Column | Type | Meaning | | --- | --- | --- | | warehouse_id | INT | Warehouse identifier (primary key) | | warehouse_name | VARCHAR(255) | Warehouse name | `products` | Column | Type | Meaning | | --- | --- | --- | | product_id | INT | Product identifier (primary key) | | product_name | VARCHAR(255) | Product name | `inventory` | Column | Type | Meaning | | --- | --- | --- | | warehouse_id | INT | References `warehouses.warehouse_id` | | product_id | INT | References `products.product_id` | | recorded_quantity | INT | Stock on record before the sales, non-negative | | actual_quantity | INT | Stock found in the physical count, non-negative | Primary key: `(warehouse_id, product_id)`. `sales` | Column | Type | Meaning | | --- | --- | --- | | sale_id | INT | Sale identifier (primary key) | | warehouse_id | INT | Warehouse the units were sold from | | product_id | INT | Product sold | | quantity_sold | INT | Units in this sale, positive | ### Output Contract - Columns, in this order: `warehouse_id`, `warehouse_name`, `product_id`, `product_name`, `recorded_quantity`, `total_sold`, `expected_remaining`, `actual_quantity`, `discrepancy`. - One row per `inventory` row whose `discrepancy` is not zero. - Sort by `ABS(discrepancy)` descending, then `warehouse_id` ascending, then `product_id` ascending. - All values are integers; no rounding. ### Example `warehouses` | warehouse_id | warehouse_name | | --- | --- | | 1 | Toronto DC | | 2 | Ottawa DC | `products` | product_id | product_name | | --- | --- | | 10 | Wool beanie | | 20 | Canvas tote | | 30 | Water bottle | `inventory` | warehouse_id | product_id | recorded_quantity | actual_quantity | | --- | --- | --- | --- | | 1 | 10 | 100 | 80 | | 1 | 20 | 50 | 45 | | 1 | 30 | 40 | 36 | | 2 | 10 | 60 | 50 | | 2 | 20 | 30 | 35 | `sales` | sale_id | warehouse_id | product_id | quantity_sold | | --- | --- | --- | --- | | 1 | 1 | 10 | 12 | | 2 | 1 | 10 | 3 | | 3 | 1 | 20 | 5 | | 4 | 2 | 10 | 5 | | 5 | 2 | 30 | 7 | Expected result: | warehouse_id | warehouse_name | product_id | product_name | recorded_quantity | total_sold | expected_remaining | actual_quantity | discrepancy | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | 1 | Toronto DC | 10 | Wool beanie | 100 | 15 | 85 | 80 | 5 | | 2 | Ottawa DC | 10 | Wool beanie | 60 | 5 | 55 | 50 | 5 | | 2 | Ottawa DC | 20 | Canvas tote | 30 | 0 | 30 | 35 | -5 | | 1 | Toronto DC | 30 | Water bottle | 40 | 0 | 40 | 36 | 4 | Warehouse 1 sold 15 beanies across two sales, so 85 should remain and 80 were counted. The pair (1, 20) is left out because 50 - 5 = 45 matches the count. The pairs (2, 20) and (1, 30) have no sales, so `total_sold` is 0. Three rows tie at an absolute discrepancy of 5 and are ordered by `warehouse_id`, then `product_id`. Sale 5 is for the pair (2, 30), which has no `inventory` row, so it does not appear. ### Constraints and Clarifications - Maximum rows: `warehouses` 100, `products` 10,000, `inventory` 10,000, `sales` 100,000. - `recorded_quantity` and `actual_quantity` are non-negative integers, `quantity_sold` is a positive integer, and none of the columns are NULL. - Each `(warehouse_id, product_id)` pair appears at most once in `inventory`, and every `warehouse_id` and `product_id` in `inventory` exists in `warehouses` and `products`. - A pair can have many sales or none. Sales for a pair with no `inventory` row are ignored. - `discrepancy` can be negative when more units were counted than expected. Negative and positive mismatches are both returned. ```hint Pairs without sales Check what an inventory row with no sales looks like after your join, and what SUM returns for it. ```

Overview: Write a SQL query that subtracts each warehouse and product pair's total sales from its recorded stock, compares the result with the physical count, and returns the mismatches ordered by size. It tests aggregating before a join, keeping rows that have no sales, and sorting on an absolute value with tie-breakers.

Each row of `inventory` records how many units of a product a warehouse had on record (`recorded_quantity`) and how many were found in a later physical count (`actual_quantity`). Every sale between the two is logged in `sales`. For each `inventory` row, compare the expected remaining stock with `actual_quantity` and return the mismatches. For each row: - `total_sold` is the sum of `sales.quantity_sold` for that `(warehouse_id, product_id)` pair, or `0` if the pair has no sales. - `expected_remaining = recorded_quantity - total_sold`. - `discrepancy = expected_remaining - actual_quantity`. Return only rows with a non-zero `discrepancy`. Tables: - `warehouses(warehouse_id INTEGER PRIMARY KEY, warehouse_name VARCHAR(255))`: one row per warehouse. - `products(product_id INTEGER PRIMARY KEY, product_name VARCHAR(255))`: one row per product. - `inventory(warehouse_id INTEGER, product_id INTEGER, recorded_quantity INTEGER, actual_quantity INTEGER)`: primary key `(warehouse_id, product_id)`. `warehouse_id` references `warehouses.warehouse_id` and `product_id` references `products.product_id`. `recorded_quantity` is the stock on record before the sales and `actual_quantity` is the stock found in the physical count. - `sales(sale_id INTEGER PRIMARY KEY, warehouse_id INTEGER, product_id INTEGER, quantity_sold INTEGER)`: one row per sale, with the warehouse the units were sold from, the product sold and the units in that sale. Output: - Columns, in this order: `warehouse_id`, `warehouse_name`, `product_id`, `product_name`, `recorded_quantity`, `total_sold`, `expected_remaining`, `actual_quantity`, `discrepancy`. - One row per `inventory` row whose `discrepancy` is not zero. - Sort by `ABS(discrepancy)` descending, then `warehouse_id` ascending, then `product_id` ascending. - All values are integers; no rounding. Constraints and clarifications: - Maximum rows: `warehouses` 100, `products` 10,000, `inventory` 10,000, `sales` 100,000. - `recorded_quantity` and `actual_quantity` are non-negative integers, `quantity_sold` is a positive integer, and none of the columns are NULL. - Each `(warehouse_id, product_id)` pair appears at most once in `inventory`, and every `warehouse_id` and `product_id` in `inventory` exists in `warehouses` and `products`. - A pair can have many sales or none. Sales for a pair with no `inventory` row are ignored. - `discrepancy` can be negative when more units were counted than expected. Negative and positive mismatches are both returned.

Tables

warehouses(warehouse_id INTEGER, warehouse_name VARCHAR(255))

products(product_id INTEGER, product_name VARCHAR(255))

inventory(warehouse_id INTEGER, product_id INTEGER, recorded_quantity INTEGER, actual_quantity INTEGER)

sales(sale_id INTEGER, warehouse_id INTEGER, product_id INTEGER, quantity_sold INTEGER)

Hints

  1. Check what an inventory row with no sales looks like after your join, and what SUM returns for it.
  2. A positive and a negative discrepancy of the same size tie on the first sort column, so the two id columns decide their order.
ShopifyData ScientistmediumData Manipulation (SQL/Python)

Inventory Count Discrepancies by Warehouse and Product

Each row of inventory records how many units of a product a warehouse had on record (recorded_quantity) and how many were found in a later physical count (actual_quantity). Every sale between the two is logged in sales. For each inventory row, compare the expected remaining stock with actual_quantity and return the mismatches.

For each row:

  • total_sold is the sum of sales.quantity_sold for that (warehouse_id, product_id) pair, or 0 if the pair has no sales.
  • expected_remaining = recorded_quantity - total_sold.
  • discrepancy = expected_remaining - actual_quantity.

Return only rows with a non-zero discrepancy.

Tables:

  • warehouses(warehouse_id INTEGER PRIMARY KEY, warehouse_name VARCHAR(255)): one row per warehouse.
  • products(product_id INTEGER PRIMARY KEY, product_name VARCHAR(255)): one row per product.
  • inventory(warehouse_id INTEGER, product_id INTEGER, recorded_quantity INTEGER, actual_quantity INTEGER): primary key (warehouse_id, product_id). warehouse_id references warehouses.warehouse_id and product_id references products.product_id. recorded_quantity is the stock on record before the sales and actual_quantity is the stock found in the physical count.
  • sales(sale_id INTEGER PRIMARY KEY, warehouse_id INTEGER, product_id INTEGER, quantity_sold INTEGER): one row per sale, with the warehouse the units were sold from, the product sold and the units in that sale.

Output:

  • Columns, in this order: warehouse_id, warehouse_name, product_id, product_name, recorded_quantity, total_sold, expected_remaining, actual_quantity, discrepancy.
  • One row per inventory row whose discrepancy is not zero.
  • Sort by ABS(discrepancy) descending, then warehouse_id ascending, then product_id ascending.
  • All values are integers; no rounding.

Constraints and clarifications:

  • Maximum rows: warehouses 100, products 10,000, inventory 10,000, sales 100,000.
  • recorded_quantity and actual_quantity are non-negative integers, quantity_sold is a positive integer, and none of the columns are NULL.
  • Each (warehouse_id, product_id) pair appears at most once in inventory, and every warehouse_id and product_id in inventory exists in warehouses and products.
  • A pair can have many sales or none. Sales for a pair with no inventory row are ignored.
  • discrepancy can be negative when more units were counted than expected. Negative and positive mismatches are both returned.

Expected Output

Tables

warehousesproductsinventorysales
warehouses
ColumnType
warehouse_idINTEGER
warehouse_nameVARCHAR(255)
products
ColumnType
product_idINTEGER
product_nameVARCHAR(255)
inventory
ColumnType
warehouse_idINTEGER
product_idINTEGER
recorded_quantityINTEGER
actual_quantityINTEGER
sales
ColumnType
sale_idINTEGER
warehouse_idINTEGER
product_idINTEGER
quantity_soldINTEGER

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions