Show Each Order's Share of Its Customer's Annual Spend

Last updated: November 29, 2026

Quick Overview

For every order in 2026, return the order total, the customer's total 2026 spend, the order's percentage share of that spend, and the order's rank by size within the customer's year.

Amazon
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Phone Screen
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


For every order in 2026, return the order total, the customer's total 2026 spend, the order's percentage share of that spend, and the order's rank by size within the customer's year.

A quick phone-screen window function question framed on Amazon retail orders. The interviewer checks that you can use an aggregate as a window function to avoid a self-join, and pair it with a ranking.

What the Interviewer Expects
  • Use SUM as a window function partitioned by customer and year
  • Avoid an ORDER BY inside the SUM window, which would make it a running total
  • Use safe division for customers whose spend is zero
  • Add a rank by order size within the same partition
  • Explain why this beats a GROUP BY plus join
Key Topics to Cover
window aggregates
SUM OVER
RANK
percent of total
partitions
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 you flag customers where one order is more than 80 percent of annual spend?
  • How would you show the running share in date order instead?
  • What happens with refunds that make totals negative?
  • How would you do this across several years at once?
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

Table orders(order_id, customer_id, order_ts, order_total, status). Use shipped orders placed in calendar year 2026. Assume totals are positive and in USD....

Approach

SUM(order_total) OVER (PARTITION BY customer_id) attaches the customer's yearly total to every order row without collapsing rows. Because I filter to 2026 first, the partition is effectively custome...


Submit Your Answer
Markdown supported