Build a Monthly Revenue Report Excluding Cancelled Orders
Show order count, revenue and average order value per month for the latest 12 months of data.
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
Show monthly order revenue for the last 12 months, excluding cancelled orders.
A correct answer returns 12 rows with 4 columns: month, orders, revenue, avg_order_value.
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
| month | orders | revenue | avg_order_value |
|---|---|---|---|
First 5 of 12 rows. This is the output of the model solution on the practice database.
What you practise
- Filtering rows with a WHERE condition
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping rows with GROUP BY to get one result row per group
- Working with dates in SQLite: strftime, julianday and DATE
- Sorting the result with ORDER BY
- Returning only the first rows with LIMIT
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Use SUBSTR to extract year-month, filter out cancelled, GROUP BY month
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
orderstable. - Filter in
WHERE: keep the rows wherestatusis none of 'cancelled' or 'refunded'. - Group the rows by
month(the year-month oforder_date), so the result has one row for each value. - For each group, compute
COUNT(*),SUM(total)andAVG(total). ROUND()trims the calculated figures to a fixed number of decimals.- Sort the result by
monthdescending. - Keep only the first 12 rows with
LIMIT.
Model solution
Checking your session…
What to watch for
SUBSTR(order_date, 1, 7) cuts YYYY-MM out of the text date. The 12 months are the latest twelve present in the data (ORDER BY month DESC LIMIT 12), not the twelve before today, which matters because this dataset ends in December 2024.
- GROUP BY. Every selected column must either be in GROUP BY or sit inside an aggregate. SQLite accepts other columns and returns an arbitrary value for them; most databases raise an error.
- Date functions. These date functions are SQLite syntax. MySQL uses DATE_FORMAT and DATEDIFF, PostgreSQL uses TO_CHAR, EXTRACT and date subtraction.
- LIMIT. LIMIT without ORDER BY returns an arbitrary set of rows. LIMIT is also dialect-specific: SQL Server uses TOP and Oracle uses FETCH FIRST.
Related SQL challenges
- Calculate Monthly Revenue with strftimeSimilar levelMedium
- Find the Top 5 Customers by Total SpendSimilar levelMedium
- Get the 5 Most Recent OrdersEasierEasy
- Find the Week with the Highest RevenueHarderHard
- Find the City with the Highest Total RevenueHarderHard
Keep learning
- Lesson: GROUP BY
- Lesson: Date Functions
- Lesson: LIMIT / TOP / FETCH
- Lesson: ORDER BY
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database