Customers with Purchases in 3 Consecutive Months
Company: McKinsey
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Given a table of purchases, return the distinct `customer_id` values of customers who made at least one purchase in each of three consecutive calendar months `M`, `M+1` and `M+2`. A run of months may cross a year boundary, so November, December and January count as consecutive.
```hint Think in months
Turn each date into one number per calendar month, chosen so that consecutive months always differ by exactly 1, December to January included.
```
### Input Table(s)
Table `purchases`:
| Column | Type | Meaning |
|---|---|---|
| purchase_id | INT | Unique ID of the purchase |
| customer_id | INT | Customer who made the purchase, never NULL |
| purchase_date | DATE | Calendar date of the purchase, never NULL |
Primary key: `purchase_id`. A customer can have many rows, including several on the same day or in the same month.
### Output Contract
- One column, `customer_id`.
- Each qualifying customer appears exactly once.
- Rows are ordered by `customer_id` ascending.
- No rounding is involved.
### Example
Input `purchases`:
| purchase_id | customer_id | purchase_date |
|---|---|---|
| 1 | 10 | 2023-11-30 |
| 2 | 10 | 2023-12-15 |
| 3 | 10 | 2024-01-02 |
| 4 | 20 | 2024-02-01 |
| 5 | 20 | 2024-02-28 |
| 6 | 20 | 2024-03-15 |
Expected output:
| customer_id |
|---|
| 10 |
Customer 10 bought in November 2023, December 2023 and January 2024, three consecutive months across a year boundary, so customer 10 qualifies. Customer 20 bought twice in February 2024 and once in March 2024, which covers only two consecutive months.
### Constraints and Clarifications
- `purchases` has 1 to 100,000 rows.
- `customer_id` is a positive integer.
- `customer_id` and `purchase_date` are never NULL.
- Several purchases in the same month count as one month of activity.
- Consecutive is decided by calendar month alone. A purchase on November 30 and one on December 1 cover two consecutive months even though they are one day apart.
- The three months must be adjacent. Purchases in January, February and April do not qualify.
- A customer with a run of four or more consecutive months is listed once.
Overview: Write a SQL query that finds customers with at least one purchase in each of three consecutive calendar months, including runs that cross a year boundary. It tests month arithmetic on dates, deduplication of repeated purchases, and self-joins or window functions.
Table `purchases` records one row per purchase:
- `purchase_id` INTEGER, primary key, unique ID of the purchase
- `customer_id` INTEGER, never NULL, a positive integer identifying the customer who made the purchase
- `purchase_date` DATE, never NULL, calendar date of the purchase
A customer can have many rows, including several on the same day or in the same month. The table has 1 to 100,000 rows.
Return the distinct `customer_id` values of customers who made at least one purchase in each of three consecutive calendar months M, M+1 and M+2. A run of months may cross a year boundary, so November, December and January count as consecutive.
Rules:
- Several purchases in the same month count as one month of activity.
- Consecutive is decided by calendar month alone. A purchase on November 30 and one on December 1 cover two consecutive months even though they are one day apart.
- The three months must be adjacent. Purchases in January, February and April do not qualify.
- A customer with a run of four or more consecutive months is listed once.
Output: one column, `customer_id`. Each qualifying customer appears exactly once. Order rows by `customer_id` ascending. No rounding is involved.
Tables
purchases(purchase_id INTEGER, customer_id INTEGER, purchase_date DATE)
Hints
- Turn each date into one number per calendar month, chosen so that consecutive months always differ by exactly 1, December to January included.
- Several purchases in the same month count as one month of activity.