The inventory feed sends repeated snapshots for each (store, item). Rows with NULL `on_hand` are corrupt and must be ignored. The current state of an item is its latest valid snapshot by `snapshot_ts`; if two valid snapshots share the same timestamp, the one with the higher `load_id` wins. Return per store: `store_id`, `items_tracked` (items with a valid current state), `items_out_of_stock` (current on_hand = 0), `units_on_hand` (sum of current on_hand). Order by `store_id`.
| Column | Type |
|---|---|
| store_id | INTEGER |
| item_id | INTEGER |
| on_hand | INTEGER |
| snapshot_ts | TIMESTAMP |
| load_id | INTEGER |
| store_id | item_id | on_hand | snapshot_ts | load_id |
|---|---|---|---|---|
| 1 | 100 | 20 | 2026-10-01 06:00:00 | 1 |
| 1 | 100 | 12 | 2026-10-02 06:00:00 | 2 |
| 1 | 101 | 5 | 2026-10-01 06:00:00 | 1 |
| 1 | 101 | 0 | 2026-10-02 06:00:00 | 2 |
| 1 | 102 | 7 | 2026-10-02 06:00:00 | 2 |
| 2 | 100 | 0 | 2026-10-01 06:00:00 | 1 |
| 2 | 100 | 9 | 2026-10-02 06:00:00 | 2 |
| 2 | 103 | 4 | 2026-10-02 06:00:00 | 2 |
| store_id | items_tracked | items_out_of_stock | units_on_hand |
|---|---|---|---|
| 1 | 3 | 1 | 19 |
| 2 | 2 | 0 | 13 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Reward Points with Expiry Resets · 90th Percentile Daily Sales by Region ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.