Briefing: the dataset and business questions
Chapter 11 — “The Message”
The main hall of Vault-9 is silent.
You've earned every clearance: reading, filters, , joins, nested rooms, time, windows, write access, blueprints, and the engine room. The tools you learned one by one now have to come together in a single report.
QUERY shuts down the side panels and brings the archive's last unopened block onto the central screen. Above it is the assignment K. left one hundred sixty years ago:
Build a complete report on Kotomarket—and the archive will release the final block.
Beside the assignment is the outline of a key made of four dark segments. Each segment corresponds to one business question. Each part of the report must light up one segment.
QUERY: No new operators, cadet. You already have everything you need. There's only one question: can you put it all together?

Your task: a complete report on Kotomarket
This is the capstone—the final project of the course.
There will be almost no new SQL syntax here. The challenge now is to turn business questions written in everyday language into precise SQL queries.
You'll build a complete analytics report for the Kotomarket online store and use everything you've already learned:
- selecting and filtering data;
- and
GROUP BY; JOIN;- CTEs;
- ;
- working with dates;
- ranking;
- conditional logic with
CASE.
We'll work with five tables:
| Table | What it stores | Important fields |
|---|---|---|
users | customers | id, full_name, email, country, city, signup_date |
products | product catalog | id, name, category, price, stock |
orders | orders | id, user_id, created_at, status, total_amount |
order_items | order line items | id, order_id, product_id, quantity, unit_price |
events | user behavior | id, user_id, product_id, event_type, created_at |
One detail in the data matters throughout this report: every order has a status field.
Possible statuses:
paid— the order has been paid for;cancelled— the order was canceled;refunded— the order was refunded;pending— the order is awaiting payment.
For this report, that detail becomes a business rule:
the store earns money only from orders with the paid status.
If you calculate revenue from every row in orders, the result will be too high. It will include canceled, refunded, and unpaid orders. The report may look convincing, but the number will be wrong for the business.
The correct definition of revenue for this chapter is:
Revenue = SUM(total_amount) from the orders table where status = 'paid'
In SQL, that becomes this template:
SELECT
SUM(total_amount) AS revenue
FROM orders
WHERE status = 'paid';
K.'s four questions
The final report has four parts. These are the four segments of the key.
1. How much did we earn, and how is revenue growing month by month?
Here you need to calculate revenue from paid orders only and break it down by month.
The main idea:
metric: SUM(total_amount)
source: orders
filter: status = 'paid'
breakdown: month of created_at
2. Which products and categories bring in the most money?
The orders table alone isn't enough here. You need the line items from order_items and product details from products.
The main idea:
metric: SUM(quantity * unit_price)
source: order_items + orders + products
filter: orders.status = 'paid'
breakdown: product, category
Why not just use products.price? Each order line already contains unit_price—the price at the time of purchase. The product's current catalog price may have changed since then.
3. Do customers come back?
Here you'll work with and : which month a user signed up, and whether they came back to make purchases later.
The main idea:
metric: share of users in the cohort who made a purchase in the next period
source: users + paid orders
filter: orders.status = 'paid'
breakdown: signup month / purchase month
Retention here is based on purchases, so you need the paid filter again.
4. Who are our best customers, and who is about to leave?
Here you'll use analysis:
- R — Recency: how long ago the customer last purchased;
- F — Frequency: how often they purchased;
- M — Monetary: how much money they brought in.
The main idea:
R: date of the most recent paid order
F: number of paid orders
M: total value of paid orders
source: users + orders
filter: orders.status = 'paid'
breakdown: user
This turns ordinary order data into customer segments.
How to break a business question into metrics
Business questions almost never arrive as ready-made SQL.
A product manager doesn't write:
SELECT DATE_TRUNC('month', created_at), SUM(total_amount)
FROM orders
WHERE status = 'paid'
GROUP BY DATE_TRUNC('month', created_at);
They write something like:
Take a look at how our sales are doing.
That's where the analyst's work begins. You need to turn a vague request into a precise definition.
Step 1. Clarify the question
“How are sales doing?” can mean many different things:
- monthly revenue;
- number of orders;
- average order value;
- weekly trends;
- comparison with the previous period;
- sales by category;
- sales from paid orders only;
- sales with or without refunds.
Until the question is clear, it's too early to write SQL. Otherwise, three analysts can produce three different reports, and all three can technically be “about sales.”
Step 2. Define the metric
A metric isn't simply a label such as “revenue” or “orders.” It should be defined clearly enough that someone else can reproduce the calculation.
A useful formula is:
Metric = aggregate + source + filter
For example:
Revenue = SUM(total_amount) from the orders table where status = 'paid'
Break it down like this:
- :
SUM(total_amount); - source:
orders; - filter:
status = 'paid'.
In SQL:
SELECT
SUM(total_amount) AS revenue
FROM orders
WHERE status = 'paid';
This definition is specific enough to verify. If another report shows a different number, you know what to compare: does it also use only paid orders? The same source? The same period?
Step 3. Choose the dimensions
A is the field you use to break a metric down.
A metric answers:
how much?
A dimension answers:
broken down by what?
Examples:
| Metric | Possible dimensions |
|---|---|
| revenue | month, country, category, city |
| number of orders | day, status, source, user |
| average order value | month, city, customer segment |
| number of customers | signup month, country, |
In SQL, dimensions usually appear in SELECT and GROUP BY, while metrics are calculated with aggregate functions.
A typical report query follows this pattern:
SELECT
<dimension>,
<aggregate> AS <metric>
FROM <fact table>
JOIN <lookups>
ON <join condition>
WHERE <metric definition filter>
GROUP BY <dimension>
ORDER BY <dimension>;
For example, monthly revenue:
SELECT
DATE_TRUNC('month', created_at) AS month,
SUM(total_amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY DATE_TRUNC('month', created_at)
ORDER BY month;
Here:
DATE_TRUNC('month', created_at)is the dimension;SUM(total_amount)is the metric;ordersis the source;status = 'paid'is the definition filter.
Capstone map
You can break K.'s four questions into metrics, dimensions, and tools before writing any SQL.
| K.'s question | Metric | Breakdown | Main tools |
|---|---|---|---|
| How much did we earn, and how are we growing? | SUM(total_amount) for paid orders | month | DATE_TRUNC, GROUP BY, LAG |
| Which products and categories bring in money? | SUM(quantity * unit_price) for paid-order line items | product, category | JOIN, GROUP BY, ROW_NUMBER |
| Do customers come back? | share of customers who return in later periods | signup cohort / purchase month | , LEFT JOIN, aggregates |
| Who are our best customers? | R, F, M per user | user / segment | CTE, NTILE, CASE |
Throughout the capstone, keep these definitions consistent from one section to the next.
If the first query calculates revenue from paid orders only, but the second calculates product sales from all orders, the numbers won't reconcile. The report will look like an archive map drawn at a different scale on every page.
Define the metric dictionary first. Then write the SQL.
Initial dataset review
Before building a report, an analyst starts by inspecting the data.
The first step is to understand the size of each table:
SELECT 'users' AS table_name, COUNT(*) AS rows FROM users
UNION ALL
SELECT 'products', COUNT(*) FROM products
UNION ALL
SELECT 'orders', COUNT(*) FROM orders
UNION ALL
SELECT 'order_items', COUNT(*) FROM order_items
UNION ALL
SELECT 'events', COUNT(*) FROM events
ORDER BY table_name;
This isn't a “pointless technical query.” It quickly shows you the scale of the data:
- how many customers there are;
- how many products there are;
- how many orders there are;
- how many order line items there are;
- whether behavioral events exist;
- whether any table looks suspiciously empty.
The second step is to inspect the order statuses:
SELECT
status,
COUNT(*) AS orders_cnt,
ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS share_pct
FROM orders
GROUP BY status
ORDER BY orders_cnt DESC, status;
There's a useful expression here:
SUM(COUNT(*)) OVER ()
At first glance, it looks odd: an aggregate inside a . The logic works like this:
COUNT(*)counts the number of orders in eachstatusgroup;SUM(COUNT(*)) OVER ()adds those group counts across the entire result;- that gives us the grand total we need to calculate each share.
This gives us both the number of orders in each status and its percentage of all orders.
For example:
| status | orders_cnt | share_pct |
|---|---|---|
| paid | 86 | 71.7 |
| refunded | 14 | 11.7 |
| cancelled | 11 | 9.2 |
| pending | 9 | 7.5 |
This result immediately shows that about 72% of orders can be treated as revenue-generating. The rest can't be included directly in revenue.
The key rule for this chapter: calculate revenue and purchase-based order metrics only with WHERE status = 'paid'.
cancelled, refunded, and pending are not store revenue. If you forget the filter, you'll inflate revenue, average order value, product contribution, monetary values, and purchase-based .
You can analyze behavioral events from events separately. But when the question is “who bought,” “how much did they buy,” or “which products brought in money,” use paid orders.
Interview question
Interview question: A product manager writes, “Take a look at how our sales are doing.” What do you do as an analyst?
Strong answer: I don't start writing SQL right away. First, I turn the vague question into a set of metrics and dimensions. I clarify the period, what counts as a sale, whether refunds should be included, whether trends matter, and which breakdowns are needed: month, category, city, channel, . Then I define the metrics—for example, revenue is SUM(total_amount) for orders with the paid status. After that, I review the data: table sizes, order statuses, status shares, possible missing values, and duplicates. Then I calculate the totals, trends, average order value, and any requested product or customer metrics. I deliver the table together with an interpretation: what grew, what declined, and where to look next.
Another common question: What's the difference between a metric and a ?
Strong answer: A metric is an aggregated number: revenue, number of orders, average order value, number of customers. A dimension is how we break that metric down: month, category, city, country, cohort, customer segment. In SQL, dimensions usually go into GROUP BY, while metrics are calculated with functions such as SUM, COUNT, AVG, MIN, and MAX.
Another common question: Why can't you calculate revenue from every order in orders?
Strong answer: Because not every order is revenue. orders may contain canceled, refunded, and unpaid orders. If you include them in SUM(total_amount), the report will overstate revenue and every metric derived from it: average order value, top products, customer monetary value, and . Revenue needs a fixed definition: count only orders with the paid status.
SELECT 'users' AS table_name, COUNT(*) AS rows FROM users
UNION ALL SELECT 'products', COUNT(*) FROM products
UNION ALL SELECT 'orders', COUNT(*) FROM orders
UNION ALL SELECT 'order_items', COUNT(*) FROM order_items
UNION ALL SELECT 'events', COUNT(*) FROM events
ORDER BY table_name;