KatInterview

Retail Spend Analytics: Aggregation and Regional Rankings

SQL & DataModel answer
MediumModel answerSQL & Datawindow-functionsaggregationCTE

Last updated

Write two SQL queries against a payment dataset to find top vendors globally and per region using aggregation, joins, and window-based dense ranking.

Retail Spend Analytics

You work with a payment processing dataset containing two tables.


transactions

columntypedescription
transaction_idintegerUnique identifier for each transaction.
user_idintegerIdentifier for the customer who made the transaction.
vendor_idintegerIdentifier for the merchant.
transaction_timetimestampWhen the transaction was recorded.
transaction_dollarsnumericDollar value of the transaction.
transaction_typetextEither PURCHASE or REFUND.
refund_transaction_idintegerFor REFUND rows only: the transaction_id of the original purchase.

vendors

columntypedescription
vendor_idintegerUnique identifier for each merchant.
citytextCity where the merchant operates.
state_provincetextState or province of the merchant.
countrytextTwo-letter country code.

Rules

  • Only PURCHASE rows represent money charged. REFUND rows should be excluded from all totals.
  • "The last 2 years" means a rolling window: any transaction recorded at or after CURRENT_TIMESTAMP - INTERVAL '2 years'.

Task 1

Find the three merchants who collected the most purchase dollars over the last 2 years, globally. Return vendor_id and total_dollars_charged, ordered from highest to lowest. Break ties by vendor_id ascending so the result is deterministic.

Task 2

For every (state_province, country) pair, identify the top 3 merchants by total purchase dollars over the same 2-year window. Return state_province, country, vendor_id, total_dollars, and vendor_rank. Rank using dense ranking so merchants with identical totals share a rank — this means a location can return more than 3 rows when there are ties at rank 3.


Write your queries in PostgreSQL syntax. Be prepared to discuss how you would adapt the date arithmetic for other dialects, and what changes if the business asks for net spend (purchases minus refunds) instead of gross spend.

Hints

Hint 1

Aggregate and filter before you join — sum transaction rows by vendor_id first, then bring in vendor location metadata. Joining first and aggregating second risks row fan-out if the join isn't strictly 1-to-1 in practice, and it also makes the intent harder to read.

Hint 2

Window functions are evaluated after WHERE and HAVING, so you cannot filter on a window-function result in the same query level where you compute it. Wrap it in a CTE or subquery, then apply the rank filter in the outer SELECT.

Hint 3

Think carefully about what dense ranking means for the output row count. If two vendors tie for third place in a region, DENSE_RANK gives both rank 3 and your WHERE vendor_rank <= 3 returns four rows for that region — that is the correct, intended behavior. Contrast this with RANK (skips numbers) and ROW_NUMBER (breaks ties arbitrarily).