Count Each Customer's Orders in a Rolling 30-Day Window
For every order, count the same user's orders in the 30 days that follow, busiest windows first.
The challenge runs on the SQLabHub practice database (SQLite) and uses the orders table. You can solve it in the browser, with nothing to install.
The task
Find users with maximum number of orders in any 30-day rolling window.
A correct answer returns 200 rows with 2 columns: user_id, cnt.
Tables used in this challenge
orders 200 rows
id INTEGER (PK) · user_id INTEGER · total REAL · discount REAL · item_count INTEGER · status TEXT · order_date TEXT · shipped_date TEXT · shipping_city TEXT · shipping_country TEXT
| id | user_id | total | discount | item_count | status | order_date | shipped_date | shipping_city | shipping_country |
|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| user_id | cnt |
|---|---|
First 5 of 200 rows. This is the output of the model solution on the practice database.
What you practise
- Filtering a range of values with BETWEEN
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping by more than one column to get a row per combination
- Combining rows from two tables on a matching key with an inner join
- Joining a table to itself under two aliases
- Working with dates in SQLite: strftime, julianday and DATE
- Sorting the result with ORDER BY
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Use self join with date range
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
orderstable, under the aliaso1. - Join
ordersagain, under the aliaso2, ono1.user_id=o2.user_id, and only whereo2.order_dateis betweeno1.order_dateand 30 days aftero1.order_date, both ends included. - Group the rows by
o1.user_idando1.order_date, so the result has one row for each combination. - For each group, compute
COUNT(*). - Sort the result by
cntdescending.
Model solution
Checking your session…
What to watch for
SQLite window frames cannot span a number of days, so the window is built with a self join on a date range. Each order is its own window start, and the count includes that order itself.
- Self join. Without a condition such as a.id < b.id, a self join pairs every row with itself and lists each pair twice.
- INNER JOIN. An inner join drops rows that have no partner on the other side, and it repeats a row once for every match, which inflates SUM and COUNT.
- GROUP BY on several columns. Each extra GROUP BY column splits the groups further. Grouping by a column that is unique per row gives back one group per row.
Related SQL challenges
- Measure Monthly Revenue Volatility per ProductSimilar levelHard
- Count Orders in Each Customer's First 7 DaysSimilar levelHard
- Count Orders per Customer per Day with a Two-Column GROUP BYEasierMedium
- Find Each Customer's Busiest Day by Order CountSimilar levelHard
- Calculate Profit Generated by Each CustomerSimilar levelHard
Same technique, other tables
More practice on this pattern with different columns:
Keep learning
- Lesson: SELF JOIN
- Lesson: INNER JOIN
- Lesson: GROUP BY Multiple Columns
- Lesson: Date Functions
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database