Skip to content
RED SAGSkills

Month-over-Month Revenue

Compute revenue per calendar month (YYYY-MM) from non-cancelled orders (status <> 'cancelled'). Then compare each month with the previous month that appears in the result.

- revenue: monthly total, ROUND(x, 2) - prev_revenue: the previous listed month's revenue, NULL for the first month - change: revenue - prev_revenue, ROUND(x, 2), NULL for the first month

Return columns month, revenue, prev_revenue, change. Order by month ascending.

Schema

CREATE TABLE orders (
  order_id INTEGER PRIMARY KEY,
  order_date TEXT NOT NULL,
  amount REAL NOT NULL,
  status TEXT NOT NULL
);

Sample data

orders

order_idorder_dateamountstatus
12026-01-051200delivered
22026-01-20800delivered
32026-02-021500delivered
42026-02-15999cancelled
52026-03-01700shipped
62026-03-28650delivered
Hint 1

Aggregate to one row per month in a CTE first.

Hint 2

LAG(col) OVER (ORDER BY month) reads the value from the row before.

Good practice for: Data Analyst, Business Analyst, Product Manager, Data Scientist

SQL (SQLite)

Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.