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
Top Products by Sales SQL
00:00
5 left

Top Products by Sales SQL

MediumSQL · PostgreSQL

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
Interviewer

Your question is Top Products by Sales SQL. Start with the requirements and the two 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.