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
Top Three Brands by Segment Spend
00:00
5 left

Top Three Brands by Segment Spend

MediumSQL · PostgreSQL

Problem

Given a table of user receipt scans, write a query to find the top three brand partners by total spend for each user segment over the last quarter at Fetch.

Assume the last quarter is the completed calendar quarter relative to the current date. Exclude scans that cannot be associated with a user segment or brand partner.

Output

  1. One row per qualifying segment and brand partner, with columns segment, brand_name, total_spend, and segment_rank.
  2. Include no more than three brands per segment, ordered by segment, rank, total spend descending, and brand name ascending.

Output details

  • Treat DATE '2026-09-15' as today's date.
  • Use an inclusive start and exclusive end for the completed quarter.
  • Exclude scans with spend_amount equal to NULL, while retaining zero and negative amounts.
  • Break equal total_spend values by brand_name ascending when assigning segment_rank.

Schema

users
ColumnTypeDescription
user_idPKINTUnique Fetch user identifier
segmentVARCHAR(50)User segmentation label
receipt_scans
ColumnTypeDescription
scan_idPKINTUnique receipt scan identifier
user_idINTUser who submitted the scan
brand_idINTBrand identified on the receipt
spend_amountDECIMAL(12,2)Purchase amount represented by the scan
scanned_atDATEDate the receipt was scanned
brand_partners
ColumnTypeDescription
brand_idPKINTUnique brand partner identifier
brand_nameVARCHAR(100)Brand partner name
Tablesusersreceipt_scansbrand_partners
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

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.