Revenue reports
A SQL round often opens with a task like these: add up money over a period, by the right key, with the right filters. A few habits decide the result: a half-open date range, no test purchases, the right revenue column (gross or net), a second sort key for ties, and zeros for months without sales. The tasks build on Filters and dates, CASE, FILTER and rates and LEFT JOIN and zeros. When single tasks feel easy, rehearse a whole round on the mock interview page: a random task, the clock running, the hints hidden.
Q01. Revenue by channel — August 2026
Section titled “Q01. Revenue by channel — August 2026”For August 2026 show, per channel: the number of transactions, gross revenue and revenue net of refunds.
Output columns: channel, transactions, gross_usd, net_usdOrder: ORDER BY gross_usd DESC, channel
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
Which timestamp and time zone define "August"?
UTC; start_time in [2026-08-01, 2026-09-01).
Do we include test purchases?
No.
What is gross and what is net of refunds?
gross = SUM(charged_usd); net = SUM(revenue_usd). revenue_usd is 0 for refunded transactions.
Trials and promo access?
Keep them: they cost 0 but still count as transactions.
About the data: transactions
- One row in transactions is one subscription period: a purchase, a renewal or a trial. Renewals of the same subscription are separate rows.
- channel is one of north_market, south_market, web, promo.
- charged_usd is what the customer paid (gross). revenue_usd is the same, but 0 if the purchase was refunded (net).
- is_test marks a merchant's test purchase; it never counts in business numbers.
A few rows
(SELECT transaction_id AS id, channel, charged_usd AS gross, revenue_usd AS net, refunded_at, is_test FROM transactions WHERE refunded_at IS NOT NULL ORDER BY transaction_id LIMIT 1)UNION ALL(SELECT transaction_id, channel, charged_usd, revenue_usd, refunded_at, is_test FROM transactions WHERE is_test ORDER BY transaction_id LIMIT 1)UNION ALL(SELECT transaction_id, channel, charged_usd, revenue_usd, refunded_at, is_test FROM transactions WHERE NOT is_test AND refunded_at IS NULL AND NOT is_trial ORDER BY transaction_id LIMIT 1);| id | channel | gross | net | refunded_at | is_test |
|---|---|---|---|---|---|
| t_000144 | north_market | 4.99 | 0.00 | 2025-09-20 20:52:05 | false |
| t_000115 | web | 79.99 | 79.99 | NULL | true |
| t_000001 | south_market | 79.99 | 79.99 | NULL | false |
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
Start with the WHERE clause: which rows belong to August, and which rows never count? Once that is right, the grouping is one line.
Say it: Every query here filters by time, so on a big table I would want an index on start_time.
Reference solution
SELECT channel, COUNT(*) AS transactions, SUM(charged_usd) AS gross_usd, SUM(revenue_usd) AS net_usdFROM transactionsWHERE NOT is_test AND start_time >= TIMESTAMP '2026-08-01' AND start_time < TIMESTAMP '2026-09-01'GROUP BY channelORDER BY gross_usd DESC, channel;Expected output
| channel | transactions | gross_usd | net_usd |
|---|---|---|---|
| north_market | 312 | 4079.18 | 4002.23 |
| south_market | 179 | 2600.41 | 2572.44 |
| web | 51 | 518.56 | 507.58 |
| promo | 8 | 0.00 | 0.00 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. Finance wants to know how much each sales channel brings in. Gross is what customers paid (charged_usd). Net is what is left after refunds (revenue_usd, which is 0 for a refunded transaction).
Steps. One pass over transactions: WHERE (no test purchases, August as a half-open interval), then GROUP BY channel, then COUNT(*) and two SUMs.
Traps.
BETWEEN '2026-08-01' AND '2026-08-31'stops at 31 August 00:00 and loses the sales of that day: 7 sales, 148.94 net. Use>=and<.BETWEEN '2026-08-01' AND '2026-09-01'fixes that end and breaks the other one: it also counts a sale made exactly at 1 September 00:00, which belongs to September. No practice sale starts at that moment, so the result looks right. Check also runs your query on hidden edge-case data, which has such a sale, and this version fails there.- Test purchases never go into business numbers. August has 18 of them, worth 217.83 net.
- Keep trials and promo access. They cost 0, but they are transactions: August has 57 trials and 8 promo rows. Without them the count would be 485, not 550.
Check. The four channels together must give SUM(revenue_usd) with the same filter and no grouping: 7082.25 from 550 transactions. The first row is north_market with a net of 4002.23.
Follow-up. “Add the share of each channel.” Put a window over the grouped query: SUM(SUM(revenue_usd)) OVER () is the total of all groups (see Shares and running totals).
SELECT channel, SUM(revenue_usd) AS net_usd, ROUND(100.0 * SUM(revenue_usd) / SUM(SUM(revenue_usd)) OVER (), 1) AS share_pctFROM transactionsWHERE NOT is_test AND start_time >= TIMESTAMP '2026-08-01' AND start_time < TIMESTAMP '2026-09-01'GROUP BY channelORDER BY net_usd DESC;| channel | net_usd | share_pct |
|---|---|---|
| north_market | 4002.23 | 56.5 |
| south_market | 2572.44 | 36.3 |
| web | 507.58 | 7.2 |
| promo | 0.00 | 0.0 |
In production. The filter is a range on start_time, so an index on transactions(start_time) serves it. One month is a small part of the table, and the planner will choose an index scan. For a query over the whole table, a sequential scan is the right choice.
Q02. Top 5 products in the last 30 days
Section titled “Q02. Top 5 products in the last 30 days”Show the five products with the highest net revenue in the last 30 days, with the product name and the merchant name.
Output columns: product_id, product_name, merchant_name, net_usdOrder: ORDER BY net_usd DESC, product_id; LIMIT 5
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
What is "today"?
2026-09-30 00:00 UTC. Never use CURRENT_DATE in these tasks.
Last 30 days exactly?
start_time >= today - 30 days and start_time < today.
Net revenue?
SUM(revenue_usd), test purchases excluded.
Ties?
Order by net revenue, then product_id.
About the data: transactions, products, merchants
- transactions.product_id → products.id; products.merchant_id → merchants.id. A transaction reaches its merchant only through products.
- The product name is products.name, the merchant name is merchants.name.
- Net revenue is SUM(revenue_usd), test purchases excluded.
A few rows
SELECT p.id AS product_id, p.name AS product_name, m.id AS merchant_id, m.name AS merchant_nameFROM products p JOIN merchants m ON m.id = p.merchant_idORDER BY p.id LIMIT 3;| product_id | product_name | merchant_id | merchant_name |
|---|---|---|---|
| prd_01 | Product 01 | mer_01 | Pocket Quest |
| prd_02 | Product 02 | mer_02 | Lumen Labs |
| prd_03 | Product 03 | mer_03 | Driftwood Labs |
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
You need both names, so first walk from transactions to products to merchants. Then write "the last 30 days" as two comparisons with today.
Say it: The joins go through primary keys, so the only index I need is on start_time.
Reference solution
SELECT t.product_id, p.name AS product_name, m.name AS merchant_name, SUM(t.revenue_usd) AS net_usdFROM transactions tJOIN products p ON p.id = t.product_idJOIN merchants m ON m.id = p.merchant_idWHERE NOT t.is_test AND t.start_time >= TIMESTAMP '2026-09-30' - INTERVAL '30 days' AND t.start_time < TIMESTAMP '2026-09-30'GROUP BY t.product_id, p.name, m.nameORDER BY net_usd DESC, t.product_idLIMIT 5;Expected output
| product_id | product_name | merchant_name | net_usd |
|---|---|---|---|
| prd_15 | Product 15 | Kite Works | 659.76 |
| prd_02 | Product 02 | Lumen Labs | 555.83 |
| prd_12 | Product 12 | Kite Works | 452.73 |
| prd_18 | Product 18 | Orbit Fitness | 393.78 |
| prd_03 | Product 03 | Driftwood Labs | 389.82 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. Which products bring in the most money right now: net revenue over the last 30 days.
Steps. transactions → products → merchants (for the names) → the 30 days before “today” → GROUP BY the product and both names → ORDER BY net_usd DESC, product_id → LIMIT 5.
Traps.
- “Today” is the literal
TIMESTAMP '2026-09-30', notCURRENT_DATE. Otherwise the answer changes every day. - Give the start of the range an explicit type:
TIMESTAMP '2026-09-30' - INTERVAL '30 days'. - Without a second sort key,
LIMITcuts an arbitrary row when two products have the same revenue.
Check. The first row is prd_15, Product 15 of the merchant Kite Works: 659.76.
Follow-up. “Show the best product of each merchant.” Number the products inside each merchant with ROW_NUMBER() OVER (PARTITION BY merchant_id ORDER BY net_usd DESC) and keep rn = 1 in the next step (see Top N per group).
In production. The same index on start_time. The joins to products and merchants go through their primary keys, which already have indexes.
Q05. Monthly net revenue per merchant, zero-filled
Section titled “Q05. Monthly net revenue per merchant, zero-filled”Show net revenue per merchant per month from January to September 2026. Every merchant must have all nine months, with 0 when there was no revenue.
Output columns: merchant_id, month (DATE, first day of month), net_usdOrder: ORDER BY merchant_id, month
Ask first
Say each question out loud before you code. Open the interviewer's answer only after you asked.
Type the questions you would ask the interviewer, and say each one out loud. The task's questions and their answers open when you end the mock.
Which month does a transaction belong to?
The month of start_time (UTC).
Net revenue?
SUM(revenue_usd), test purchases excluded.
Merchants without products or without revenue?
Still listed, with 0 in every month.
About the data: transactions, products, merchants
- The month of a transaction is date_trunc('month', start_time).
- A merchant with no sales exists only in merchants: start from merchants and attach sales with a LEFT JOIN.
- All months, even empty ones, come from a calendar: generate_series(…, INTERVAL '1 month').
"Today" is 2026-09-30 00:00 UTC. All times are UTC. Every run is rolled back, so you cannot break the data.
-- your query here
Hint: what the interviewer would nudge
Which table lists every merchant, even one with no sales? Start from it, build the nine months with generate_series, and only then attach the revenue.
Say it: For a monthly report I would keep monthly aggregates, so the report does not read every transaction.
Reference solution
WITH months AS ( SELECT CAST(m AS DATE) AS month FROM generate_series(TIMESTAMP '2026-01-01', TIMESTAMP '2026-09-01', INTERVAL '1 month') AS g(m)),revenue AS ( SELECT p.merchant_id, CAST(date_trunc('month', t.start_time) AS DATE) AS month, SUM(t.revenue_usd) AS net_usd FROM transactions t JOIN products p ON p.id = t.product_id WHERE NOT t.is_test AND t.start_time >= TIMESTAMP '2026-01-01' AND t.start_time < TIMESTAMP '2026-10-01' GROUP BY 1, 2)SELECT m.id AS merchant_id, mo.month, COALESCE(r.net_usd, 0) AS net_usdFROM merchants mCROSS JOIN months moLEFT JOIN revenue r ON r.merchant_id = m.id AND r.month = mo.monthORDER BY m.id, mo.month;Expected output
| merchant_id | month | net_usd |
|---|---|---|
| mer_01 | 2026-01-01 | 95.91 |
| mer_01 | 2026-02-01 | 130.86 |
| mer_01 | 2026-03-01 | 196.86 |
| mer_01 | 2026-04-01 | 55.92 |
| mer_01 | 2026-05-01 | 189.87 |
| mer_01 | 2026-06-01 | 203.85 |
| mer_01 | 2026-07-01 | 257.82 |
| mer_01 | 2026-08-01 | 198.81 |
| mer_01 | 2026-09-01 | 264.81 |
| mer_02 | 2026-01-01 | 1023.42 |
| mer_02 | 2026-02-01 | 808.46 |
| mer_02 | 2026-03-01 | 789.46 |
| mer_02 | 2026-04-01 | 1041.45 |
| mer_02 | 2026-05-01 | 842.42 |
| mer_02 | 2026-06-01 | 920.31 |
| mer_02 | 2026-07-01 | 1083.30 |
| mer_02 | 2026-08-01 | 802.36 |
| mer_02 | 2026-09-01 | 1187.38 |
| mer_03 | 2026-01-01 | 644.76 |
| mer_03 | 2026-02-01 | 309.83 |
WalkthroughTry the task first: the walkthrough is most useful after your own attempt.
What it is for. A report for merchants: revenue per month, where a month without sales shows 0 instead of disappearing.
Steps.
- A CTE
revenue: net revenue permerchant_idanddate_trunc('month', start_time), for January–September. - A calendar of months from
generate_series. merchants CROSS JOIN monthsgives every pair. ThenLEFT JOIN revenueandCOALESCE(net_usd, 0).
Traps.
- Start from
merchants, not fromtransactions. Otherwise a merchant without sales disappears from the report. - Keep the filters on transactions inside the CTE. A
WHEREonrevenueafter theLEFT JOINremoves the zero rows again.
Check. 108 rows: 12 merchants × 9 months. In the practice data every merchant sells in every month, so a query without the calendar also returns 108 rows. Check also runs your query on hidden edge-case data, where one merchant has a month with no sales. Without the calendar that row is missing, and Check fails.
Short on time? Describe the pattern instead of typing every detail: “I’ll build a calendar with generate_series, cross join it with merchants, and left join the revenue, so missing months become zero.”
In production. An index on start_time serves the filter by year. For a monthly report, keep monthly totals in a summary table or a materialized view, so the report does not read every transaction.
Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.