Skip to content
RED SAGSkills

Repeat Buyers Within 30 Days

A customer is a quick repeat buyer if any of their orders was placed at most 30 days after the order immediately before it. Two orders on the same day count (0 days apart). Orders on the same date are ordered by order_id.

Return columns customer_id, listing each qualifying customer once. Row order does not matter.

Schema

CREATE TABLE orders (
  order_id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  order_date TEXT NOT NULL,
  amount REAL NOT NULL
);

Sample data

orders

order_idcustomer_idorder_dateamount
1112026-01-0150
2112026-03-1575
3122026-01-1020
4122026-02-0535
5132026-02-0190
6112026-04-2015
7142026-01-0160
8142026-03-0110
9142026-03-3112
Hint 1

Use LAG to put each order next to the same customer's previous order.

Hint 2

julianday(a) - julianday(b) is the gap in days.

Good practice for: Data Analyst, Digital Marketing Specialist, Product Manager, Data Scientist

SQL (SQLite)

Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.