Final project: a monthly report and a sales ranking
To finish the course you build two reports that land on desks in every business: the monthly numbers for management and a ranking for the head of sales. Each query pulls together several tables, groups, conditions and window functions.
You need: JOIN, GROUP BY, CASE WHEN, date functions, CTEs with WITH, RANK and SUM with OVER. You have practised every one of them. This time you decide in which order they fit together.
The situation
The office supply shop from the exercises has six months behind it, January to June 2026. Two requests are waiting. Management wants to see each month how many orders came in, how many customers ordered for the first time, how much revenue the orders brought, and what share of the half year each month accounts for. The head of sales wants to know, for the half-year reviews, who led in the second quarter and how each rep did compared with the first.
The numbers live in three tables: orders (date, status, the employee who handled it), order_items (quantity and unit price per product) and employees. The revenue of one item is quantity * unit_price. Cancelled orders never count.
The project
Part 1: the monthly report for management
Sketch it on paper first, in the order the database works in: which rows do you need, how do you spot a new customer, how do you roll everything up into months, and when does the share come in? The share is the last step, not the first.
Task Management wants the same overview every month. Build it with WITH. One row per month with the columns month (YYYY-MM), order_count (number of distinct orders), new_customers (customers whose first non-cancelled order falls in that month; not the customer_since column), revenue (sum of quantity times unit_price, rounded to two decimals) and revenue_share (the month's share of the revenue of all months, in percent, rounded to one decimal). Get the revenue of all months with a window function. Cancelled orders don't count anywhere, and a month without new customers shows 0. Sort by month, oldest first.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
Part 2: a sales ranking that compares quarters
Now it is about people rather than months. The trick is getting both quarters side by side in one row, just like a pivot table.
Task For the half-year review, the head of sales wants a ranking of the people in the Sales department. One row per person: rep (first and last name with a space, such as Julia Reyes), q1_revenue (January to March 2026), q2_revenue (April to June 2026), revenue_change (q2_revenue minus q1_revenue) and sales_rank (position by q2_revenue, highest revenue is 1, using RANK). Revenue is quantity times unit_price of the items on the orders that person handled. Cancelled orders don't count, and a quarter without revenue counts as 0. Build the query with a CTE, round amounts to two decimals and sort by sales_rank.
employees 10 rows, 7 columns
| employee_id | first_name | last_name | department | salary | hire_date | manager_id |
|---|---|---|---|---|---|---|
| 1 | Karen | Mitchell | Management | 93600 | 2018-01-02 | NULL |
| 2 | Tom | Bradley | Sales | 62400 | 2019-04-01 | 1 |
| 3 | Julia | Reyes | Sales | 46800 | 2021-06-15 | 2 |
| 4 | Marcus | Allen | Sales | 49200 | 2020-09-01 | 2 |
| 5 | Sarah | Long | Warehouse | 39600 | 2022-02-01 | 1 |
| 6 | Felix | Turner | Warehouse | 35400 | 2024-03-18 | 5 |
| 7 | Nina | Cooper | Accounting | 52800 | 2019-10-01 | 1 |
| 8 | David | Ross | Customer service | 38400 | 2023-05-02 | 2 |
| 9 | Laura | Hughes | Customer service | NULL | 2025-09-01 | 2 |
| 10 | Paul | Ward | Accounting | 43200 | 2022-11-14 | 7 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
What matters
Condense first, compare second
Window functions such as SUM(…) OVER () and RANK work on the rows that already exist. To get each month's share of the half year, you first need one row per month. That is the CTE's job: it turns 36 non-cancelled items into six monthly rows, and only the outer query works with them. Here is the intermediate result for the first three months when you run the CTE on its own:
SELECT substr(o.order_date, 1, 7) AS month,
COUNT(DISTINCT o.order_id) AS order_count,
SUM(i.quantity * i.unit_price) AS revenue
FROM orders o
JOIN order_items i ON i.order_id = o.order_id
WHERE o.status <> 'cancelled'
GROUP BY substr(o.order_date, 1, 7);
| month | order_count | revenue |
|---|---|---|
| 2026-01 | 3 | 1304.7 |
| 2026-02 | 2 | 1557.6 |
| 2026-03 | 3 | 3249.25 |
Use these numbers as a checkpoint. If yours match, the new customers and the share are still to come.
New customers: find the first date, then compare
The orders table doesn't know whether an order is a customer's first. You work that out in a separate step: MIN(order_date) per customer_id is the day of the first order. Join that result back to the orders and you can ask, row by row, whether the order date is exactly that first day. The customer_since column won't help here; it says when someone entered the customer list, not when they first ordered.
COUNT(DISTINCT) after a JOIN
After the JOIN, each order appears once for every item it contains, so COUNT(*) counts items. COUNT(DISTINCT o.order_id) gives you the number of orders.
Conditional sums instead of two queries
SUM(CASE WHEN o.order_date < '2026-04-01' THEN i.quantity * i.unit_price ELSE 0 END) only adds up first-quarter items. A second sum for the second quarter puts both values in the same row. The ELSE 0 matters: without it, a sum with no matching rows becomes NULL rather than 0.
Months from a date
Dates are stored as text in the form YYYY-MM-DD, so the first seven characters are the month: substr(order_date, 1, 7) or strftime('%Y-%m', order_date). MySQL, PostgreSQL and SQL Server use different date functions; the overview is in the lesson MySQL vs PostgreSQL vs SQL Server.
If you get stuck
- Run the CTE on its own first and compare it with the table above. If the intermediate result matches, the problem is in the outer query.
- If February and May come out too high, the filter on cancelled orders is missing.
- If May shows NULL instead of 0 new customers, you attached them with a LEFT JOIN. Wrap them in COALESCE(…, 0), or count them with COUNT(DISTINCT CASE …) right inside the monthly CTE.
- If the shares don't add up to roughly 100, you are not dividing by the revenue of all months. SUM(revenue) OVER () with nothing inside the brackets runs over every row.
- If David Ross shows up in the ranking, the filter on the Sales department is missing. He works in customer service.
- If someone has an empty value instead of 0 for the second quarter, the CASE is missing ELSE 0.
- Both projects are also checked against different data. Typing names, months or amounts straight into the query will fail for that reason.
What you have learned in this course
Take one more look at your solution: it reads from several tables, filters, groups, calculates with conditions and compares rows with each other. That is the toolkit used in finance, sales and data analysis. What is still missing is practice on data you haven't seen before, and that is what the final test checks.
FAQ
How many points is the project worth?
Each of the two project tasks counts three times as much as a single exercise, because you solve many steps in one go.
Is the project free?
No. The projects from level 2 onwards are part of Premium, this one included. All of level 1 is free, and so is the first exercise on every other page.
Is there only one right answer?
No. The result is checked, not the text of your query. substr or strftime for the month, RANK or DENSE_RANK for the position: with this data they are equally good. Only WITH and a window function with OVER must appear, because that is what the task is about.
Why compare quarters instead of each rep's previous month?
Not every rep has orders every month. LAG would then return the last month with revenue, not the previous calendar month, and the comparison would be off. Quarters built with conditional sums don't have that problem.
What comes after the project?
The final test. It covers the material from every level.