Top Merchant Category per Card Member With Ties

Last updated: November 29, 2026

Quick Overview

Given a transactions table (card_member_id, txn_date, merchant_category, amount_usd, status) and refunds recorded as negative amounts, write SQL that returns, for each card member, the merchant category with the highest net spend in the last full calendar month, keeping all categories tied for first.

American Express
Data Manipulation (SQL/Python)
Data Engineer
American Express
November 29, 2026
Data Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given a transactions table (card_member_id, txn_date, merchant_category, amount_usd, status) and refunds recorded as negative amounts, write SQL that returns, for each card member, the merchant category with the highest net spend in the last full calendar month, keeping all categories tied for first.

American Express data teams build spend summaries that feed year-end statements, offer targeting and Membership Rewards category bonuses. The interviewer is checking date filtering, net vs gross spend, window functions and whether you think about declined authorizations and ties.

What the Interviewer Expects
  • Filters to the previous full calendar month, not the last 30 days
  • Excludes declined or reversed transactions and nets refunds
  • Aggregates before ranking
  • Uses RANK or DENSE_RANK so ties are kept
  • Explains how members with only refunds are handled
Key Topics to Cover
Window functions
RANK vs ROW_NUMBER
Date truncation
Refund netting
Aggregation
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would the query change if refunds arrive in a separate table and can post in a later month than the purchase?
  • How would you make this incremental for a daily pipeline over billions of rows?
  • What if a member should only get a category if net spend is positive?
  • How would you bucket merchant categories when a merchant changes its MCC?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Ask: does status include declined and pending rows? (Assume values POSTED, DECLINED, REVERSED; keep POSTED.) Are refunds negative amounts in the same table, dated when they post? (Assume yes...

Approach
  1. Compute the first day of the current month and of the previous month.
  2. Filter posted rows in that range.
  3. Sum amount_usd by member and category to get net spend.
  4. Rank categories within eac...

Submit Your Answer
Markdown supported

Related Questions