MediumSQL challenge #107DATEGROUP BYSUBSTRFILTER

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.

Solve This SQL Challenge

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

First 3 rows of the orders table
iduser_idtotaldiscountitem_countstatusorder_dateshipped_dateshipping_cityshipping_country
1432638.110.253cancelled2024-04-24NULLLos AngelesUSA
2829470.195delivered2024-03-152023-04-09ChicagoUSA
3136683.240.082delivered2023-02-152023-07-10Los AngelesUSA

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
monthordersrevenueavg_order_value
2024-12410140.712535.18
2024-11617380.722896.79
2024-1023926.431963.21
2024-0914319.464319.46
2024-08723737.273391.04

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:

  1. Read from the orders table.
  2. Filter in WHERE: keep the rows where status is none of 'cancelled' or 'refunded'.
  3. Group the rows by month (the year-month of order_date), so the result has one row for each value.
  4. For each group, compute COUNT(*), SUM(total) and AVG(total).
  5. ROUND() trims the calculated figures to a fixed number of decimals.
  6. Sort the result by month descending.
  7. 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.
Solve This SQL Challenge

Keep learning