
You can now join tables and build a revenue report. You know who bought what, how much customers spent, and which orders count towards your totals.
Then the next questions arrive: “Is revenue growing? Which product leads each category? What was each customer's previous purchase?
These questions need another layer of SQL.
This is Part 3 of our three-part series. We will use window functions to compare rows, handle a missing month before calculating growth.
By the end, you will have a monthly performance report. The aim is to build queries you can explain, validate, and improve.
Our monthly revenue report from Part 2 contains January, February, March, May, and June. April is missing because no orders were placed that month.
If we immediately calculate “previous month” using those rows, May will compare with March. The arithmetic may be correct, but the label will be misleading.
Advanced SQL often begins with questions like these:
The functions are useful. Understanding the result is what makes them useful in practice.
Connect SQLTools to the same demoDb database from Part 1 and create part-3.sql. All four required tables and all sample records were prepared in that first setup. Do not rerun the setup script.
We will keep using completed-order revenue in GBP: quantity multiplied by the historical unit price, excluding cancelled and refunded orders.
The January–June total is £759. April contributes zero. That gives us a useful check as we build more complicated reports.