Supply chain planners need a one-line health check per warehouse before the festive restock.
From warehouse_stock, return one row per warehouse_code with:
sku_count— number of SKUs stockedtotal_units— total units on handavg_unit_price— average unit price rounded to 2 decimalsmin_unitsandmax_units— smallest and largest unit count for that warehouse
Sample data
Table: warehouse_stock
14 row(s) · showing first 8
| stock_idINT | warehouse_codeTEXT | skuTEXT | unitsINT | unit_priceNUMERIC(10,2) |
|---|---|---|---|---|
| 1 | WH-NORTH | SKU-1001 | 120 | 249.50 |
| 2 | WH-NORTH | SKU-1002 | 40 | 1299.00 |
| 3 | WH-NORTH | SKU-1003 | 310 | 89.99 |
| 4 | WH-NORTH | SKU-1004 | 75 | 549.25 |
| 5 | WH-WEST | SKU-1001 | 200 | 249.50 |
| 6 | WH-WEST | SKU-1005 | 15 | 3499.00 |
| 7 | WH-WEST | SKU-1006 | 480 | 35.75 |
| 8 | WH-SOUTH | SKU-1002 | 90 | 1299.00 |
warehouse_stock
- stock_idINT
- warehouse_codeTEXT
- skuTEXT
- unitsINT
- unit_priceNUMERIC(10,2)
- Step 1: Collapse the rows to one per warehouse with `GROUP BY warehouse_code`.
- Step 2: `COUNT(sku)` gives the number of stocked SKUs in that warehouse.
- Step 3: `SUM(units)` totals the physical inventory, while `MIN(units)` and `MAX(units)` expose the thinnest and deepest lines.
- Step 4: `AVG(unit_price)` returns many decimal places on a `NUMERIC` column, so wrap it in `ROUND(..., 2)` to match the requested precision.
- Step 5: Alias every aggregate so the output columns are self-describing.
SELECT warehouse_code,
COUNT(sku) AS sku_count,
SUM(units) AS total_units,
ROUND(AVG(unit_price), 2) AS avg_unit_price,
MIN(units) AS min_units,
MAX(units) AS max_units
FROM warehouse_stock
GROUP BY warehouse_code;Learn the concept first
Stuck on Aggregate Functions? These free SQL lessons teach it step by step.
More Aggregate Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program