Monthly Revenue and Unique Customers from Transactions

Quick Overview

Write a SQL query that groups transactions by calendar month and reports total revenue and the number of distinct paying customers for each month. It tests date formatting, grouping on a derived year-month key, and the difference between counting rows and counting distinct customers.

Monthly Revenue and Unique Customers from Transactions

Company: Goldman Sachs

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For each calendar month that has at least one row in `Transactions`, report the month as a `YYYY-MM` string, the total revenue for that month (the sum of `amount`), and the number of distinct customers who made at least one purchase that month. Return the months in ascending order. ### Input Table(s) `Transactions` | Column | Type | Meaning | | --- | --- | --- | | `transaction_id` | INT | Primary key, one row per transaction | | `customer_id` | INT | Customer who made the purchase, never NULL | | `transaction_date` | DATE | Date of the purchase, never NULL | | `amount` | DECIMAL(10,2) | Purchase amount, never NULL and never negative | Primary key: `transaction_id`. A customer can have many transactions, including several in the same month or on the same day. ### Output Contract | Column | Type | Meaning | | --- | --- | --- | | `month` | TEXT | Year and month in `YYYY-MM` format, for example `2024-01` | | `total_revenue` | DECIMAL | Sum of `amount` over the month's transactions, kept at two decimal places with no extra rounding | | `unique_customers` | INTEGER | Count of distinct `customer_id` values with a transaction that month | Return one row per month that appears in the data, ordered by `month` ascending. `month` is unique in the output, so this order is fully determined. ### Example Input `Transactions`: | transaction_id | customer_id | transaction_date | amount | | --- | --- | --- | --- | | 1 | 101 | 2023-12-30 | 15.00 | | 2 | 101 | 2024-01-05 | 50.00 | | 3 | 102 | 2024-01-17 | 30.00 | | 4 | 101 | 2024-01-28 | 20.00 | | 5 | 103 | 2024-02-02 | 75.50 | | 6 | 103 | 2024-02-20 | 0.00 | Expected output: | month | total_revenue | unique_customers | | --- | --- | --- | | 2023-12 | 15.00 | 1 | | 2024-01 | 100.00 | 2 | | 2024-02 | 75.50 | 1 | Customer 101 bought twice in January 2024 and counts once for that month, and also counts in December 2023. January revenue is 50.00 + 30.00 + 20.00 = 100.00. Customer 103's zero-amount purchase still counts as a purchase in February. ### Constraints and Clarifications - The table holds 1 to 100,000 transactions. - `transaction_id` is unique, and no column contains NULL. - `amount` is a non-negative decimal, and a transaction with `amount` of 0 still counts toward `unique_customers`. - Months with no transactions do not appear in the output. Do not generate empty months. - The same month in different years forms separate groups: January 2023 and January 2024 are two rows. ```hint Grouping key Pick a grouping key that keeps the year as well as the month, and check how a customer with several purchases in one month gets counted. ```

Overview: Write a SQL query that groups transactions by calendar month and reports total revenue and the number of distinct paying customers for each month. It tests date formatting, grouping on a derived year-month key, and the difference between counting rows and counting distinct customers.

Goldman Sachs logo
Goldman Sachs
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

For each calendar month that has at least one row in Transactions, report the month as a YYYY-MM string, the total revenue for that month (the sum of amount), and the number of distinct customers who made at least one purchase that month. Return the months in ascending order.

Input Table(s)

Transactions

ColumnTypeMeaning
transaction_idINTPrimary key, one row per transaction
customer_idINTCustomer who made the purchase, never NULL
transaction_dateDATEDate of the purchase, never NULL
amountDECIMAL(10,2)Purchase amount, never NULL and never negative

Primary key: transaction_id. A customer can have many transactions, including several in the same month or on the same day.

Output Contract

ColumnTypeMeaning
monthTEXTYear and month in YYYY-MM format, for example 2024-01
total_revenueDECIMALSum of amount over the month's transactions, kept at two decimal places with no extra rounding
unique_customersINTEGERCount of distinct customer_id values with a transaction that month

Return one row per month that appears in the data, ordered by month ascending. month is unique in the output, so this order is fully determined.

Example

Input Transactions:

transaction_idcustomer_idtransaction_dateamount
11012023-12-3015.00
21012024-01-0550.00
31022024-01-1730.00
41012024-01-2820.00
51032024-02-0275.50
61032024-02-200.00

Expected output:

monthtotal_revenueunique_customers
2023-1215.001
2024-01100.002
2024-0275.501

Customer 101 bought twice in January 2024 and counts once for that month, and also counts in December 2023. January revenue is 50.00 + 30.00 + 20.00 = 100.00. Customer 103's zero-amount purchase still counts as a purchase in February.

Constraints and Clarifications

  • The table holds 1 to 100,000 transactions.
  • transaction_id is unique, and no column contains NULL.
  • amount is a non-negative decimal, and a transaction with amount of 0 still counts toward unique_customers .
  • Months with no transactions do not appear in the output. Do not generate empty months.
  • The same month in different years forms separate groups: January 2023 and January 2024 are two rows.