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
November 29, 20260
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
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- 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 ProblemsSample 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
- Compute the first day of the current month and of the previous month.
- Filter posted rows in that range.
- Sum
amount_usdby member and category to get net spend. - Rank categories within eac...