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.
Why this question comes up
Finance wants a twelve-month revenue trend for the monthly business review. Cancelled and refunded orders never turned into money, so they are left out; counting them would make the busiest months look better than they were.
The task
Show monthly order revenue for the last 12 months in the data, excluding cancelled and refunded 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
- Matching a column against a list of values with IN
- 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 and refunded orders, 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.
Show the model solution
SELECT SUBSTR(order_date, 1, 7) AS month,
COUNT(*) AS orders,
ROUND(SUM(total), 2) AS revenue,
ROUND(AVG(total), 2) AS avg_order_value
FROM orders
WHERE status NOT IN ('cancelled', 'refunded')
GROUP BY month
ORDER BY month DESC
LIMIT 12;This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.
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.
- IN. NOT IN returns no rows at all if the list contains a NULL. With literal lists that is easy to avoid; with a subquery it is a common trap.
Related SQL challenges
- Count Orders per User in the Latest Year of DataSimilar levelMedium
- Find the Top 15 Customers by Lifetime RevenueSimilar levelMedium
- Find the Week with the Highest RevenueHarderHard
- Monthly Revenue Running Total with a CTE and Correlated SubqueryHarderHard
- Find the City with the Highest Total RevenueHarderHard
Keep learning
- Lesson: GROUP BY
- Lesson: Date Functions
- Lesson: WHERE IN
- Lesson: LIMIT / TOP / FETCH
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database