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.
- The report runs on 15 April 2025, so last month is March 2025: every purchase from
2025-03-01 00:00:00up to and including2025-03-31 23:59:59. Purchases in February or April do not count at all. - A day is a calendar date. Several purchases on the same date are one day, whatever their times.
- Keep customers with exactly 3 different purchase days in March: not 2, not 4.
- For each of them return
purchase_count: how many purchases they made in March (every purchase, not every day).
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
- C1: two purchases on 2 March are one day; with 10 and 31 March that is 3 days from 4 purchases. The 31 March purchase is at 21:45, so a filter that stops at
'2025-03-31'(midnight) would lose it. - C2: 4 different days, so not exactly 3.
- C3: 28 February is outside last month. 1 March 00:00:00 is inside, but that gives only 2 March days.
- C4: 3 purchases, but on only 2 days (8 March twice).
- C5: 4, 18 and 25 March are 3 days; 1 April 00:00:00 is already this month, so
purchase_countis 3, not 4. - C7: 3 days in February and 1 in March. Only March matters: 1 day.
Notes
purchase_tsis a DATETIME, not a DATE: think about what'2025-03-31'means when it is compared with2025-03-31 21:45:00.- In production you would derive the month from
CURRENT_DATE; here the report date is fixed (15 April 2025) so the answer does not change with the day you run it. - Column names must be exactly
customer_idandpurchase_count. The query runs on MySQL 8.
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
- COUNT DISTINCT
- HAVING
- DATE()
- Date range
- DATETIME boundaries
- E-commerce
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:
- 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.
- DATE(purchase_ts) turns a date-time into a calendar day; COUNT(DISTINCT DATE(purchase_ts)) counts days, not purchases.
- Filters on a group's count go in HAVING: HAVING COUNT(DISTINCT DATE(purchase_ts)) = 3.
- 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
- Monthly Revenue Report Using DATE_FORMAT
- Count Employees Hired in Each Year
- Find the Month With the Highest Number of New Hires
- Zomato: A User's Orders from the Last 30 Days
- Amazon: Customers Active on Exactly 3 Days Last Month (PySpark)
- IBM: Average VM Uptime Hours per Region
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.