Calculate the percentage of first-month buyers who purchase again in the next calendar month

Quick Overview

Given a table of user transactions, compute what percentage of the buyers in the earliest calendar month also bought in the following month. It tests deriving month boundaries from a table-wide minimum date, distinct user counts, a left join between two user sets, and safe division with rounding.

Calculate the percentage of first-month buyers who purchase again in the next calendar month

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `transactions` table records every purchase. Month 1 is the calendar month of the earliest transaction in the table, and Month 2 is the calendar month immediately after it. Compute the percentage of distinct Month 1 buyers who also purchased in Month 2. Return a single column `retention_percentage`, rounded to 2 decimal places, or `NULL` if no users purchased in Month 1. ```hint Fix the months first Month 1 comes from the whole table, not from each user's first purchase. Work out its boundaries once, then compare the users seen in each month. ``` ### Input Table(s) `transactions` | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Id of the transaction. Primary key. | | user_id | INT | The buyer. NOT NULL. | | transaction_date | DATE | Day of the purchase. NOT NULL. | | amount | DECIMAL(10,2) | Amount paid. NOT NULL. | Primary key: `transaction_id`. A user can have any number of rows, including several in the same month. ### Output Contract - Exactly one row with one column, `retention_percentage`. - The value is the number of distinct users who bought in both Month 1 and Month 2, divided by the number of distinct users who bought in Month 1, times 100, rounded to 2 decimal places. - The value is `NULL` when Month 1 has no buyers. - The result is always a single row, so no ordering is needed. ### Example Input `transactions`: | transaction_id | user_id | transaction_date | amount | |---|---|---|---| | 1 | 101 | 2024-01-05 | 50.00 | | 2 | 102 | 2024-01-10 | 75.00 | | 3 | 103 | 2024-01-15 | 30.00 | | 4 | 104 | 2024-01-20 | 90.00 | | 5 | 101 | 2024-02-03 | 40.00 | | 6 | 102 | 2024-02-12 | 60.00 | | 7 | 105 | 2024-02-25 | 20.00 | Expected result: | retention_percentage | |---| | 50.00 | Month 1 is January 2024, with 4 distinct buyers: 101, 102, 103 and 104. Users 101 and 102 also bought in February 2024 (Month 2). User 105 bought only in February and is not part of the calculation. Retention is 2 / 4 × 100 = 50.00. ### Constraints and Clarifications - `transactions` holds 1 to 100,000 rows. - `transaction_id` is unique, `user_id` is a positive integer, `transaction_date` is a valid date and `amount` is non-negative. No column contains NULL. - Months are calendar months. If the earliest transaction is in December 2024, Month 2 is January 2025. - A user with several purchases in a month counts once for that month. - Users who did not buy in Month 1 do not enter the numerator or the denominator, and transactions after Month 2 are ignored. - Every row counts as a purchase, whatever its amount. - With at least one row in the table, Month 1 always has a buyer. The `NULL` rule still has to hold, so guard the division.

Overview: Given a table of user transactions, compute what percentage of the buyers in the earliest calendar month also bought in the following month. It tests deriving month boundaries from a table-wide minimum date, distinct user counts, a left join between two user sets, and safe division with rounding.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The transactions table records every purchase. Month 1 is the calendar month of the earliest transaction in the table, and Month 2 is the calendar month immediately after it. Compute the percentage of distinct Month 1 buyers who also purchased in Month 2. Return a single column retention_percentage, rounded to 2 decimal places, or NULL if no users purchased in Month 1.

Input Table(s)

transactions

ColumnTypeMeaning
transaction_idINTId of the transaction. Primary key.
user_idINTThe buyer. NOT NULL.
transaction_dateDATEDay of the purchase. NOT NULL.
amountDECIMAL(10,2)Amount paid. NOT NULL.

Primary key: transaction_id. A user can have any number of rows, including several in the same month.

Output Contract

  • Exactly one row with one column, retention_percentage .
  • The value is the number of distinct users who bought in both Month 1 and Month 2, divided by the number of distinct users who bought in Month 1, times 100, rounded to 2 decimal places.
  • The value is NULL when Month 1 has no buyers.
  • The result is always a single row, so no ordering is needed.

Example

Input transactions:

transaction_iduser_idtransaction_dateamount
11012024-01-0550.00
21022024-01-1075.00
31032024-01-1530.00
41042024-01-2090.00
51012024-02-0340.00
61022024-02-1260.00
71052024-02-2520.00

Expected result:

retention_percentage
50.00

Month 1 is January 2024, with 4 distinct buyers: 101, 102, 103 and 104. Users 101 and 102 also bought in February 2024 (Month 2). User 105 bought only in February and is not part of the calculation. Retention is 2 / 4 × 100 = 50.00.

Constraints and Clarifications

  • transactions holds 1 to 100,000 rows.
  • transaction_id is unique, user_id is a positive integer, transaction_date is a valid date and amount is non-negative. No column contains NULL.
  • Months are calendar months. If the earliest transaction is in December 2024, Month 2 is January 2025.
  • A user with several purchases in a month counts once for that month.
  • Users who did not buy in Month 1 do not enter the numerator or the denominator, and transactions after Month 2 are ignored.
  • Every row counts as a purchase, whatever its amount.
  • With at least one row in the table, Month 1 always has a buyer. The NULL rule still has to hold, so guard the division.