KatInterview

Daily and Category-Level New Customer Purchase Rate

SQL & DataModel answer
MediumModel answerSQL & Datasqlpandaswindow-functions

Last updated

Flag each order as a customer's first in a category or overall, then aggregate to compute new-buyer proportions by day and category.

Daily and Category-Level New Customer Purchase Rate

You work on the analytics team for an e-commerce platform. Leadership wants to understand how much of each day's order volume comes from customers making their very first purchase — both within a specific product category and across the platform as a whole.

You have access to an orders table:

+----------+------------+----------+-------------+
| order_id | date       | category | customer_id |
+----------+------------+----------+-------------+
| 100      | 2025-01-01 | Books    | 1           |
| 101      | 2025-01-02 | Books    | 1           |
| 102      | 2025-01-01 | Music    | 2           |
| 103      | 2025-01-03 | Music    | 3           |
| 104      | 2025-01-03 | Books    | 2           |
+----------+------------+----------+-------------+

Columns:

  • order_id — unique identifier for each order; higher values indicate later placement when orders fall on the same day
  • date — calendar date of the order (day granularity)
  • category — product category of the order
  • customer_id — identifier for the purchasing customer

What to compute

Metric 1 — by day and category: For every (date, category) pair, what proportion of orders in that group represent the placing customer's first-ever order in that category?

Metric 2 — by day: For every calendar day, what proportion of that day's orders represent a customer's first order across any category?

Expected output

Metric 1:

+------------+----------+--------------+-------------+-------------------------+
| date       | category | total_orders | first_orders| first_order_proportion  |
+------------+----------+--------------+-------------+-------------------------+
| 2025-01-01 | Books    | 1            | 1           | 1.0                     |
| 2025-01-01 | Music    | 1            | 1           | 1.0                     |
| 2025-01-02 | Books    | 1            | 0           | 0.0                     |
| 2025-01-03 | Books    | 1            | 1           | 1.0                     |
| 2025-01-03 | Music    | 1            | 1           | 1.0                     |
+------------+----------+--------------+-------------+-------------------------+

Metric 2:

+------------+--------------+-------------+------------------------+
| date       | total_orders | first_orders | first_order_proportion |
+------------+--------------+-------------+------------------------+
| 2025-01-01 | 2            | 2           | 1.0                    |
| 2025-01-02 | 1            | 0           | 0.0                    |
| 2025-01-03 | 2            | 1           | 0.5                    |
+------------+--------------+-------------+------------------------+

Requirements and assumptions

  • "First order in a category" means the chronologically earliest order for that customer in that category, based on the full history of the table — not just the current reporting window.
  • "First order overall" means the chronologically earliest order for that customer across all categories.
  • When two orders from the same customer fall on the same date, use order_id as a tie-breaker (lower ID = earlier).
  • The proportion denominator is total orders in the group, not total customers.
  • You may write your solution in SQL, Python (pandas), or both.

Hints

Hint 1

Start by assigning each order a rank within its partition before doing any grouping. For Metric 1 the partition is (customer_id, category); for Metric 2 it is customer_id alone. A rank of 1 means it is the first order in that partition.

Hint 2

Once you have a 0/1 flag column, the proportion is just the mean of that flag within the reporting group. This avoids writing a separate numerator subquery and a denominator subquery and then joining them.

Hint 3

Watch out for two classic traps: same-day ties (use order_id as a secondary sort key, not just date) and computing flags after filtering to a date range (a customer who bought last year will look like a new customer if you exclude that history before flagging).