Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Top Spending Categories Query
00:00
5 left

Top Spending Categories Query

MediumSQL · PostgreSQL

Problem

ING wants a quarterly view of spending patterns among customers who were active during the most recent completed quarter. For this exercise, treat Q2 2025 as the reporting quarter, from 2025-04-01 inclusive through 2025-07-01 exclusive.

Write a PostgreSQL query to identify the three highest-spending transaction categories for ING users whose last_active_at falls within Q2 2025. Include only transactions occurring in the same quarter. Treat NULL transaction amounts as contributing zero, exclude transactions without a category, and calculate net spending by summing transaction amounts.

Requirements

  1. Identify active users with a CTE or equivalent subquery.
  2. Join active users to their Q2 2025 transactions.
  3. Aggregate spending by category, order from highest to lowest total, and return exactly three categories.
  4. Use a deterministic secondary sort by category name.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique ING customer identifier
full_nameVARCHAR(100)Customer name
last_active_atDATEDate of the customer's most recent activity
transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
user_idINTEGERCustomer who made the transaction
transaction_dateDATEDate of the transaction
categoryVARCHAR(50)Spending category
amountNUMERIC(12,2)Net transaction amount, including refunds
Tablesuserstransactions
Interviewer

Your question is Top Spending Categories Query. 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.