Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Query for Top Products
00:00
5 left

SQL Query for Top Products

MediumSQL · PostgreSQL

Problem

Business context

Natural Intelligence uses product-level revenue reporting to evaluate performance across its comparison and betting surfaces. Assume the report is run on 2025-01-15, so the last completed quarter is Q4 2024.

Task

Write a PostgreSQL query that returns the top five products by completed revenue during the last completed calendar quarter.

Requirements

  1. Join products to their categories and transactions.
  2. Include only transactions with status = 'completed' within the quarter ending before the reporting date.
  3. Aggregate revenue by product, order from highest to lowest revenue, and use product_id as a deterministic tie-breaker.

Schema

ni_products
ColumnTypeDescription
product_idPKINTUnique Natural Intelligence product identifier
product_nameVARCHAR(100)Product or partner brand name
category_idINTIdentifier of the product category
ni_product_categories
ColumnTypeDescription
category_idPKINTUnique category identifier
category_nameVARCHAR(100)Product category label
ni_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
product_idINTProduct associated with the transaction
transaction_dateDATEDate on which the transaction occurred
revenueNUMERIC(12,2)Revenue attributed to the transaction
statusVARCHAR(20)Transaction processing status
Tablesni_productsni_product_categoriesni_transactions
Interviewer

Your question is SQL Query for Top Products. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.