Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Spending Categories Query

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Spending Categories Query. Start with the requirements and the two tables on the right.

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results