Skip to content

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.

SQLQ01 · Difficulty: easy · Target time: 5 min

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.

  1. Which timestamp and time zone define "August"?

    UTC; start_time in [2026-08-01, 2026-09-01).

  2. Do we include test purchases?

    No.

  3. What is gross and what is net of refunds?

    gross = SUM(charged_usd); net = SUM(revenue_usd). revenue_usd is 0 for refunded transactions.

  4. 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);
idchannelgrossnetrefunded_atis_test
t_000144north_market4.990.002025-09-20 20:52:05false
t_000115web79.9979.99NULLtrue
t_000001south_market79.9979.99NULLfalse

"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.

Reference solution
SELECT channel,
COUNT(*) AS transactions,
SUM(charged_usd) AS gross_usd,
SUM(revenue_usd) AS net_usd
FROM transactions
WHERE NOT is_test
AND start_time >= TIMESTAMP '2026-08-01'
AND start_time < TIMESTAMP '2026-09-01'
GROUP BY channel
ORDER BY gross_usd DESC, channel;

Expected output

channeltransactionsgross_usdnet_usd
north_market3124079.184002.23
south_market1792600.412572.44
web51518.56507.58
promo80.000.00
4 rows
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_pct
FROM transactions
WHERE NOT is_test
AND start_time >= TIMESTAMP '2026-08-01'
AND start_time < TIMESTAMP '2026-09-01'
GROUP BY channel
ORDER BY net_usd DESC;
channelnet_usdshare_pct
north_market4002.2356.5
south_market2572.4436.3
web507.587.2
promo0.000.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.

SQLQ02 · Difficulty: easy · Target time: 7 min

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.

  1. What is "today"?

    2026-09-30 00:00 UTC. Never use CURRENT_DATE in these tasks.

  2. Last 30 days exactly?

    start_time >= today - 30 days and start_time < today.

  3. Net revenue?

    SUM(revenue_usd), test purchases excluded.

  4. 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_name
FROM products p JOIN merchants m ON m.id = p.merchant_id
ORDER BY p.id LIMIT 3;
product_idproduct_namemerchant_idmerchant_name
prd_01Product 01mer_01Pocket Quest
prd_02Product 02mer_02Lumen Labs
prd_03Product 03mer_03Driftwood 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.

Reference solution
SELECT t.product_id,
p.name AS product_name,
m.name AS merchant_name,
SUM(t.revenue_usd) AS net_usd
FROM transactions t
JOIN products p ON p.id = t.product_id
JOIN merchants m ON m.id = p.merchant_id
WHERE 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.name
ORDER BY net_usd DESC, t.product_id
LIMIT 5;

Expected output

product_idproduct_namemerchant_namenet_usd
prd_15Product 15Kite Works659.76
prd_02Product 02Lumen Labs555.83
prd_12Product 12Kite Works452.73
prd_18Product 18Orbit Fitness393.78
prd_03Product 03Driftwood Labs389.82
5 rows
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', not CURRENT_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, LIMIT cuts 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”
SQLQ05 · Difficulty: medium · Target time: 12 min

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.

  1. Which month does a transaction belong to?

    The month of start_time (UTC).

  2. Net revenue?

    SUM(revenue_usd), test purchases excluded.

  3. 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.

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_usd
FROM merchants m
CROSS JOIN months mo
LEFT JOIN revenue r ON r.merchant_id = m.id AND r.month = mo.month
ORDER BY m.id, mo.month;

Expected output

merchant_idmonthnet_usd
mer_012026-01-0195.91
mer_012026-02-01130.86
mer_012026-03-01196.86
mer_012026-04-0155.92
mer_012026-05-01189.87
mer_012026-06-01203.85
mer_012026-07-01257.82
mer_012026-08-01198.81
mer_012026-09-01264.81
mer_022026-01-011023.42
mer_022026-02-01808.46
mer_022026-03-01789.46
mer_022026-04-011041.45
mer_022026-05-01842.42
mer_022026-06-01920.31
mer_022026-07-011083.30
mer_022026-08-01802.36
mer_022026-09-011187.38
mer_032026-01-01644.76
mer_032026-02-01309.83
108 rows (first 20 shown)
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.

  1. A CTE revenue: net revenue per merchant_id and date_trunc('month', start_time), for January–September.
  2. A calendar of months from generate_series.
  3. merchants CROSS JOIN months gives every pair. Then LEFT JOIN revenue and COALESCE(net_usd, 0).

Traps.

  • Start from merchants, not from transactions. Otherwise a merchant without sales disappears from the report.
  • Keep the filters on transactions inside the CTE. A WHERE on revenue after the LEFT JOIN removes 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.