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 5 Products SQL Query
00:00
5 left

Top 5 Products SQL Query

MediumSQL · PostgreSQL

Problem

Jerry wants to identify the products generating the most sales on its customer and policy surfaces. Write a PostgreSQL query that returns the top five products by sales during the rolling 12-month period ending on August 29, 2026.

Requirements

  1. Include only orders with status = 'completed'.
  2. Include orders from DATE '2025-08-29' through DATE '2026-08-29'.
  3. Calculate sales as quantity * unit_price, treating a missing unit price as zero.
  4. Return product name and total sales, sorted from highest to lowest. Break ties by product_id ascending.

Output

  1. One row per product, with columns product_name and total_sales.
  2. Include only completed orders within the specified one-year period.
  3. Treat a NULL unit price as zero.
  4. Sort by total sales descending, break ties by product ID ascending, and return at most five rows.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Jerry product name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
order_dateDATEDate the order was placed
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTUnique order-line identifier
order_idINTReferences orders.order_id
product_idINTReferences products.product_id
quantityINTNumber of units purchased
unit_priceNUMERIC(10,2)Price per unit at checkout
Tablesproductsordersorder_items
Interviewer

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