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
Start free
Top Spending Categories SQL
00:00
5 left

Top Spending Categories SQL

MediumSQL · PostgreSQL

Problem

Brex finance teams need a recent view of company spending by category. Write a PostgreSQL query that finds the top three categories for each company using net spend after refunds, evaluated as of 2025-03-31.

Requirements

  1. Include transactions from the 30-day period from 2025-03-02 through 2025-03-31, inclusive.
  2. Aggregate refunds by transaction before joining them to card transactions, because a transaction may have multiple refunds.
  3. Treat missing refunds as zero and calculate net_spend as transaction amount minus refunded amount.
  4. Aggregate net spend by company and category, then return the top three categories per company using a window function.
  5. Include ties only when they fall within the first three row positions, and sort the final output by company name and descending net spend.

Output details

  • Return columns in this order: company_name, category, net_spend, and category_rank.
  • Filter refunds from 2025-03-02 inclusive through 2025-04-01 exclusive.
  • Replace a NULL category with Uncategorized.
  • Break equal net_spend values by ascending category when assigning ranks.
  • Sort the final rows by ascending company_name, then ascending category_rank.
  • Do not round net_spend; retain the numeric precision produced by the calculations.

Schema

companies
ColumnTypeDescription
company_idPKINTUnique Brex customer company identifier
company_nameVARCHAR(120)Company display name
card_transactions
ColumnTypeDescription
transaction_idPKINTUnique card transaction identifier
company_idINTCompany associated with the transaction
categoryVARCHAR(80)Spending category
amountNUMERIC(12,2)Original positive transaction amount
transaction_dateDATEDate the transaction posted
refunds
ColumnTypeDescription
refund_idPKINTUnique refund identifier
transaction_idINTTransaction receiving the refund
amountNUMERIC(12,2)Refund amount
refunded_atDATEDate the refund posted
Tablescompaniescard_transactionsrefunds
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.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.