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

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.

The `inventory` table has one row per stocked item in a warehouse. Write a query that groups `inventory` by `category` and `warehouse_id` and returns, for each group, the total stock (`total_stock`) and the average price rounded to 2 decimal places (`avg_price`). Order the result by `category` ascending, then `warehouse_id` ascending. Table `inventory`: | Column | Type | Meaning | | --- | --- | --- | | id | INTEGER | Row identifier (primary key) | | item_name | VARCHAR(100) | Item name, not null | | category | VARCHAR(50) | Item category, non-empty, not null | | warehouse_id | INTEGER | Warehouse holding the item, positive, not null | | stock | INTEGER | Units in stock, non-negative, not null | | price | DECIMAL(10,2) | Unit price, positive, not null | Output columns, in this order: `category`, `warehouse_id`, `total_stock`, `avg_price`. - Return 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. 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 ignores `stock` as a weight: each row contributes its `price` once. - Each (`category`, `warehouse_id`) pair appears once in the output, so the sort order is unique.

Tables

inventory(id INTEGER, item_name VARCHAR(100), category VARCHAR(50), warehouse_id INTEGER, stock INTEGER, price DECIMAL(10,2))

Hints

  1. Check that every column in your SELECT list is either part of the grouping or inside an aggregate.
  2. A row with zero stock still belongs to its group, so it counts toward that group's average price.
CloudflareData ScientisteasyData Manipulation (SQL/Python)

Total Stock and Average Price by Category and Warehouse

The inventory table has one row per stocked item in a warehouse. Write a query that groups inventory by category and warehouse_id and returns, for each group, the total stock (total_stock) and the average price rounded to 2 decimal places (avg_price). Order the result by category ascending, then warehouse_id ascending.

Table inventory:

ColumnTypeMeaning
idINTEGERRow identifier (primary key)
item_nameVARCHAR(100)Item name, not null
categoryVARCHAR(50)Item category, non-empty, not null
warehouse_idINTEGERWarehouse holding the item, positive, not null
stockINTEGERUnits in stock, non-negative, not null
priceDECIMAL(10,2)Unit price, positive, not null

Output columns, in this order: category, warehouse_id, total_stock, avg_price.

  • Return 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.

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 ignores stock as a weight: each row contributes its price once.
  • Each (category, warehouse_id) pair appears once in the output, so the sort order is unique.

Expected Output

Tables

inventory
inventory
ColumnType
idINTEGER
item_nameVARCHAR(100)
categoryVARCHAR(50)
warehouse_idINTEGER
stockINTEGER
priceDECIMAL(10,2)

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