InterviewPrepKit

Home / SQL / Aggregation & Grouping

Average Price Per Category

easy
Solving tips
  • AVG ignores NULLs entirely — it divides the sum by the count of non-NULL values, not by the row count.
  • Wrap AVG in ROUND(expr, 2) to control the number of decimal places in the output.
  • GROUP BY the category, then order by the aggregate alias in ORDER BY.

Given a product catalog, compute the average price within each category.

Schema

CREATE TABLE products (
  product_id INTEGER PRIMARY KEY,
  category   TEXT NOT NULL,
  price      NUMERIC   -- NULL means price not yet set
);

Sample data:

product_idcategoryprice
1Books10
2Books20
3Books30
4Electronics100
5Electronics300
6ElectronicsNULL
7Toys50

Task

For each category, return:

  • category
  • avg_price — the average of price in that category, rounded to 2 decimal places

Order the result by avg_price descending.

Expected output

categoryavg_price
Electronics200.00
Toys50.00
Books20.00
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.