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

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.

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

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

ColumnTypeMeaning
warehouse_idINTWarehouse identifier (primary key)
warehouse_nameVARCHAR(255)Warehouse name

products

ColumnTypeMeaning
product_idINTProduct identifier (primary key)
product_nameVARCHAR(255)Product name

inventory

ColumnTypeMeaning
warehouse_idINTReferences warehouses.warehouse_id
product_idINTReferences products.product_id
recorded_quantityINTStock on record before the sales, non-negative
actual_quantityINTStock found in the physical count, non-negative

Primary key: (warehouse_id, product_id).

sales

ColumnTypeMeaning
sale_idINTSale identifier (primary key)
warehouse_idINTWarehouse the units were sold from
product_idINTProduct sold
quantity_soldINTUnits 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_idwarehouse_name
1Toronto DC
2Ottawa DC

products

product_idproduct_name
10Wool beanie
20Canvas tote
30Water bottle

inventory

warehouse_idproduct_idrecorded_quantityactual_quantity
11010080
1205045
1304036
2106050
2203035

sales

sale_idwarehouse_idproduct_idquantity_sold
111012
21103
31205
42105
52307

Expected result:

warehouse_idwarehouse_nameproduct_idproduct_namerecorded_quantitytotal_soldexpected_remainingactual_quantitydiscrepancy
1Toronto DC10Wool beanie1001585805
2Ottawa DC10Wool beanie60555505
2Ottawa DC20Canvas tote3003035-5
1Toronto DC30Water bottle40040364

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.