Amazon: Customers Active on Exactly 3 Days Last Month

SQL coding challenge · Difficulty: easy · Topic: Date Functions · +50 XP

Problem

*Amazon-style interview practice.*

Topic: Date Functions · COUNT(DISTINCT ...) · GROUP BY / HAVING

Amazon's retention team wants to know who shops regularly but not daily. Find every customer who made purchases on exactly three different days last month, and how many purchases they made in that month.

Return customer_id and purchase_count (the grader checks the rows, not their order).

Schema

Table: purchases

| Column | Type | Meaning |
| --- | --- | --- |
| purchase_id | INT | Unique id of the purchase |
| customer_id | VARCHAR | The customer who paid |
| purchase_ts | DATETIME | When the purchase was made (date and time) |
| amount | DECIMAL | Order value in rupees |

Tables

Table: purchases

| purchase_id | customer_id | purchase_ts | amount |
| --- | --- | --- | --- |
| 1 | C7 | 2025-02-03 10:00:00 | 410.00 |
| 2 | C7 | 2025-02-11 13:30:00 | 95.00 |
| 3 | C7 | 2025-02-25 18:15:00 | 1200.00 |
| 4 | C3 | 2025-02-28 23:30:00 | 1299.00 |
| 5 | C3 | 2025-03-01 00:00:00 | 220.00 |
| 6 | C1 | 2025-03-02 10:15:00 | 499.00 |
| 7 | C1 | 2025-03-02 18:40:00 | 120.00 |
| 8 | C5 | 2025-03-04 16:20:00 | 1599.00 |
| 9 | C2 | 2025-03-05 11:00:00 | 799.00 |
| 10 | C4 | 2025-03-08 09:00:00 | 300.00 |
| 11 | C4 | 2025-03-08 20:00:00 | 450.00 |
| 12 | C7 | 2025-03-09 11:45:00 | 330.00 |
| 13 | C1 | 2025-03-10 09:05:00 | 2499.00 |
| 14 | C2 | 2025-03-12 19:30:00 | 150.00 |
| 15 | C3 | 2025-03-15 12:00:00 | 640.00 |
| 16 | C5 | 2025-03-18 10:10:00 | 210.00 |
| 17 | C4 | 2025-03-19 14:25:00 | 89.00 |
| 18 | C2 | 2025-03-20 08:45:00 | 999.00 |
| 19 | C5 | 2025-03-25 21:00:00 | 560.00 |
| 20 | C2 | 2025-03-28 22:10:00 | 75.00 |
| 21 | C1 | 2025-03-31 21:45:00 | 349.00 |
| 22 | C5 | 2025-04-01 00:00:00 | 999.00 |

Expected Output

| customer_id | purchase_count |
| --- | --- |
| C1 | 4 |
| C5 | 3 |

Explanation

Notes

What this SQL challenge teaches you

“Amazon: Customers Active on Exactly 3 Days Last Month” is a easy-level SQL challenge focused on Date Functions. Working through it gives you hands-on practice with COUNT DISTINCT, HAVING, DATE(), Date range, DATETIME boundaries, E-commerce — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Filter last month first: purchase_ts >= '2025-03-01' AND purchase_ts < '2025-04-01'. A half-open range keeps every time on 31 March.
  2. DATE(purchase_ts) turns a date-time into a calendar day; COUNT(DISTINCT DATE(purchase_ts)) counts days, not purchases.
  3. Filters on a group's count go in HAVING: HAVING COUNT(DISTINCT DATE(purchase_ts)) = 3.
  4. COUNT(*) in the same SELECT gives purchase_count, the number of purchases in March.

Interview practice

This problem is good practice for data engineering interviews at companies such as Amazon. Interviewers use problems like it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated easy and covers Date Functions.

Solve this challenge free on PySpark.in