Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.