Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products by Sales SQL

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

Your question is Top Products by Sales 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

Catalina uses product sales data to evaluate product performance across retail campaigns. Write a PostgreSQL query that identifies the three products with the highest sales revenue during the last 12 months.

Assume the query runs on 2026-08-29, so the reporting period begins on 2025-08-29. Include products with no qualifying sales in the intermediate calculation, but return only the top three products.

Requirements

  1. Join products to sales while preserving products that have no sales.
  2. Include only sales dated from CURRENT_DATE - INTERVAL '1 year' through CURRENT_DATE.
  3. Calculate total sales revenue per product, treating a missing total as zero.
  4. Rank products by total revenue descending and return exactly the top three, using product ID as a deterministic tie-breaker.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product display name
sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTProduct associated with the sale
sale_dateDATEDate on which the sale occurred
sale_amountNUMERIC(12,2)Revenue attributed to the sale
Tablesproductssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results