Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top-Performing Products Query

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

Your question is Top-Performing Products Query. 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

Tesco's commercial team wants to identify the best-performing products in a specific quarter using product sales data.

Task

Write a PostgreSQL query that returns the top three Tesco products by revenue for Q2 2025, from 1 April through 30 June 2025.

Requirements

  1. Join the product catalogue to sales records and restrict sales to Q2 2025 using an inclusive start date and exclusive end date.
  2. Calculate total units sold and revenue for each product, using quantity * unit_price for revenue.
  3. Rank products by revenue in descending order and return the top three products, including ties if they occur.
  4. Exclude products with no positive Q2 revenue and sort the final output by rank.

Schema

products
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(150)Tesco product name
categoryVARCHAR(80)Product category
sales
ColumnTypeDescription
sale_idPKINTEGERUnique sale identifier
product_idINTEGERProduct sold, referencing products.product_id
sale_dateDATEDate on which the sale occurred
quantityINTEGERNumber of units sold
unit_priceNUMERIC(10,2)Selling price per unit
Tablesproductssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results