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_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL
order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.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_idfirst_namelast_namedepartmentsalaryhire_datemanager_id
1KarenMitchellManagement936002018-01-02NULL
2TomBradleySales624002019-04-011
3JuliaReyesSales468002021-06-152
4MarcusAllenSales492002020-09-012
5SarahLongWarehouse396002022-02-011
6FelixTurnerWarehouse354002024-03-185
7NinaCooperAccounting528002019-10-011
8DavidRossCustomer service384002023-05-022
9LauraHughesCustomer serviceNULL2025-09-012
10PaulWardAccounting432002022-11-147
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL
order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.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);
monthorder_countrevenue
2026-0131304.7
2026-0221557.6
2026-0333249.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.

Up next: the final test

You have worked through every level. In the final test you show that you can write queries without a template, from SELECT all the way to window functions.

Go to the final test

Write to us

Choose language

Select your preferred language