Max Orders in 14-Day Window
Find users with highest order count in any 14-day period.
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 highest order count in any 14-day period.
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
self join 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 14 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
- 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
- Count Each Customer's Orders in a Rolling 30-Day WindowSimilar levelHard
- Count Orders in a Rolling 7-Day WindowSimilar levelHard
- Count Orders per Customer per Day with a Two-Column GROUP BYEasierMedium
- Measure Monthly Revenue Volatility per ProductSimilar levelHard
- Find Each Customer's Busiest Day by Order CountSimilar levelHard
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