Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Average Sales by Category SQL

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Average Sales by Category SQL. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Business Context

Quantiphi's commerce analytics team needs category-level sales benchmarks for product performance reporting. Categories with no completed sales should remain visible for dashboard completeness.

Task

Write a PostgreSQL query that calculates the average completed sale value for each product category during Q1 2025. A sale's value is quantity * unit_price.

Requirements

  1. Join products to sales by product_id and retain categories with no qualifying sales.
  2. Include only sales with status completed and a sale date from 2025-01-01 through 2025-03-31.
  3. Return the category, number of completed sale rows, and average sale value rounded to two decimal places. Ignore NULL sale values using PostgreSQL aggregate behavior.
  4. Sort the output alphabetically by category.

Schema

products
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
sales
ColumnTypeDescription
sale_idPKINTEGERUnique sale identifier
product_idINTEGERProduct associated with the sale
sale_dateDATEDate on which the sale occurred
statusVARCHAR(20)Sale processing status
quantityINTEGERNumber of units sold
unit_priceNUMERIC(10,2)Price for one unit
Tablesproductssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results