Compute total stock and average price per category and warehouse
Company: Cloudflare
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `inventory` table has one row per stocked item in a warehouse. Group `inventory` by `category` and `warehouse_id`, computing total stock (`total_stock`) and average price rounded to 2 decimal places (`avg_price`). Order by `category` ascending, then `warehouse_id` ascending.
### Input Table(s)
`inventory`
| Column | Type | Meaning |
| --- | --- | --- |
| id | INT | Row identifier (primary key) |
| item_name | VARCHAR(100) | Item name, not null |
| category | VARCHAR(50) | Item category, non-empty, not null |
| warehouse_id | INT | Warehouse holding the item, positive, not null |
| stock | INT | Units in stock, non-negative, not null |
| price | DECIMAL(10,2) | Unit price, positive, not null |
Primary key: `id`.
### Output Contract
- Columns, in this order: `category`, `warehouse_id`, `total_stock`, `avg_price`.
- One row per `(category, warehouse_id)` combination that appears in `inventory`.
- `total_stock` is the sum of `stock` in the group.
- `avg_price` is the plain average of `price` over the rows in the group, rounded to 2 decimal places.
- Sort by `category` ascending, then `warehouse_id` ascending.
### Example
`inventory`
| id | item_name | category | warehouse_id | stock | price |
| --- | --- | --- | --- | --- | --- |
| 1 | USB-C cable | Accessories | 1 | 120 | 9.99 |
| 2 | Laptop sleeve | Accessories | 1 | 40 | 24.50 |
| 3 | Phone case | Accessories | 2 | 75 | 14.00 |
| 4 | 27-inch monitor | Displays | 1 | 10 | 249.99 |
| 5 | 24-inch monitor | Displays | 1 | 0 | 179.99 |
| 6 | Portable monitor | Displays | 2 | 5 | 199.00 |
| 7 | Webcam | Accessories | 1 | 30 | 49.50 |
Expected result:
| category | warehouse_id | total_stock | avg_price |
| --- | --- | --- | --- |
| Accessories | 1 | 190 | 28.00 |
| Accessories | 2 | 75 | 14.00 |
| Displays | 1 | 10 | 214.99 |
| Displays | 2 | 5 | 199.00 |
For Accessories in warehouse 1, the stock is 120 + 40 + 30 = 190 and the average price is 83.99 / 3 = 27.9967, which rounds to 28.00. The 24-inch monitor has no stock but still counts toward the Displays average for warehouse 1: (249.99 + 179.99) / 2 = 214.99.
### Constraints and Clarifications
- `inventory` has at most 10,000 rows.
- No column is NULL. `stock` is a non-negative integer, and `price` is positive with up to 2 decimal places.
- Rows with `stock = 0` are still part of their group and count toward `avg_price`.
- The average is not weighted by stock: each row contributes its `price` once.
- Each `(category, warehouse_id)` pair appears once in the output, so the sort order is unique.
```hint Grouping key
Check that every column in your SELECT list is either part of the grouping or inside an aggregate.
```
Overview: Write a SQL query that groups an inventory table by category and warehouse and returns the total stock and the average item price rounded to two decimals, in a fixed order. It tests grouping on two columns, combining SUM and AVG in one query, and rounding an aggregate result.