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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
SQL Revenue Filtering Query
00:00
5 left

SQL Revenue Filtering Query

MediumSQL · PostgreSQL

Problem

A retail analytics team needs a calendar-year revenue report by product category. Write a PostgreSQL query that calculates category revenue and the number of distinct orders contributing to that revenue for the anchored calendar year 2026. Include only categories whose total revenue exceeds 5,000.

Output

  1. One row per qualifying product category, with columns category_name, total_revenue, and order_count
  2. Revenue is the sum of quantity * price_per_unit; rows with a NULL quantity or price are ignored by the aggregate
  3. Orders with NULL dates are excluded, as are orders outside calendar year 2026
  4. Include only categories with total revenue greater than 5,000
  5. Sort by total_revenue descending, then category_name ascending

Schema

categories
ColumnTypeDescription
category_idPKINTPrimary key for a product category
category_nameVARCHAR(100)Product category name
products
ColumnTypeDescription
product_idPKINTPrimary key for a product
product_nameVARCHAR(150)Product name
category_idINTCategory containing the product
orders
ColumnTypeDescription
order_idPKINTPrimary key for an order
order_dateDATEDate on which the order was placed
order_items
ColumnTypeDescription
order_item_idPKINTPrimary key for an order line
order_idINTOrder containing the line item
product_idINTProduct sold on the line
quantityINTNumber of units sold
price_per_unitNUMERIC(12,2)Unit selling price
Tablesordersorder_itemsproductscategories
Interviewer

Your question is SQL Revenue Filtering Query. Start with the requirements and the four 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.