Summarize each merchant's Q3 2024 transactions by average amount, count and total

Quick Overview

Write a SQL query that joins merchants to their transactions, keeps only Q3 2024 activity, and reports each merchant's average transaction amount, transaction count and total amount. It tests date-range filtering, grouped aggregation, rounding and a deterministic sort with a tiebreaker.

Summarize each merchant's Q3 2024 transactions by average amount, count and total

Company: Adyen

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a `merchants` table and a `transactions` table, summarize each merchant's activity in Q3 2024 (2024-07-01 to 2024-09-30 inclusive). For each merchant, compute the average transaction amount, the number of transactions and the total amount over that quarter. ```hint Quarter boundaries Check that transactions dated 2024-07-01 and 2024-09-30 are counted and that a transaction dated 2024-10-01 is not. ``` ### Input Table(s) `merchants` | Column | Type | Meaning | |---|---|---| | merchant_id | INT | Merchant identifier. Primary key. | | merchant_name | VARCHAR(100) | Merchant name. NOT NULL, no uniqueness constraint. | | category | VARCHAR(50) | Merchant category. May be NULL. Not needed for this task. | | registration_date | DATE | Date the merchant registered. May be NULL. Not needed for this task. | Key: `merchant_id` is the primary key. `transactions` | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Transaction identifier. Primary key. | | merchant_id | INT | Merchant the transaction belongs to. Foreign key to `merchants.merchant_id`. | | amount | DECIMAL(10,2) | Transaction amount. NOT NULL and always positive. | | transaction_date | DATE | Date of the transaction. NOT NULL. | Keys: `transaction_id` is the primary key, and `merchant_id` references `merchants.merchant_id`. ### Output Contract Return one row per merchant that has at least one transaction dated in Q3 2024. Merchants with no Q3 2024 transactions do not appear. | Column | Meaning | |---|---| | merchant_id | The merchant's id. | | merchant_name | The merchant's name. | | avg_transaction_amount | Average `amount` of the merchant's Q3 2024 transactions, rounded to 2 decimal places. | | transaction_count | Number of the merchant's Q3 2024 transactions. | | total_amount | Sum of `amount` over the merchant's Q3 2024 transactions, rounded to 2 decimal places. | Order the rows by `avg_transaction_amount` descending (the rounded value shown in the output), then by `merchant_id` ascending. Because `merchant_id` is unique, this order is fully determined. ### Example `merchants` | merchant_id | merchant_name | category | registration_date | |---|---|---|---| | 1 | Alpha Books | Retail | 2023-01-15 | | 2 | Beta Cafe | Food | 2023-06-01 | | 3 | Gamma Travel | Travel | 2024-02-10 | | 4 | Delta Games | Gaming | 2024-05-20 | `transactions` | transaction_id | merchant_id | amount | transaction_date | |---|---|---|---| | 101 | 1 | 50.00 | 2024-07-01 | | 102 | 1 | 30.00 | 2024-08-15 | | 103 | 1 | 200.00 | 2024-10-01 | | 104 | 2 | 40.00 | 2024-09-30 | | 105 | 2 | 35.00 | 2024-07-20 | | 106 | 2 | 45.00 | 2024-08-02 | | 107 | 3 | 25.50 | 2024-08-01 | | 108 | 3 | 10.25 | 2024-09-05 | | 109 | 3 | 12.00 | 2024-07-11 | | 110 | 4 | 99.99 | 2024-06-30 | Expected output: | merchant_id | merchant_name | avg_transaction_amount | transaction_count | total_amount | |---|---|---|---|---| | 1 | Alpha Books | 40.00 | 2 | 80.00 | | 2 | Beta Cafe | 40.00 | 3 | 120.00 | | 3 | Gamma Travel | 15.92 | 3 | 47.75 | Transaction 103 falls on 2024-10-01 and transaction 110 on 2024-06-30, so both are outside the quarter, and merchant 4 has no Q3 transactions left. Merchants 1 and 2 both average 40.00, so merchant 1 comes first by `merchant_id`. Merchant 3 averages 47.75 / 3 = 15.9166..., which rounds to 15.92. ### Constraints and Clarifications - `merchants` has at most 10,000 rows and `transactions` has at most 100,000 rows. - `amount` is never NULL and always positive. `transaction_date` is never NULL and always a valid date. - Every `merchant_id` in `transactions` references an existing merchant. - `transaction_id` is unique, so no transaction appears twice. - Two merchants can have the same `merchant_name`. They are still different merchants and get separate rows. - Ties on the rounded `avg_transaction_amount` are broken by `merchant_id` ascending.

Overview: Write a SQL query that joins merchants to their transactions, keeps only Q3 2024 activity, and reports each merchant's average transaction amount, transaction count and total amount. It tests date-range filtering, grouped aggregation, rounding and a deterministic sort with a tiebreaker.

Adyen logo
Adyen
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a merchants table and a transactions table, summarize each merchant's activity in Q3 2024 (2024-07-01 to 2024-09-30 inclusive). For each merchant, compute the average transaction amount, the number of transactions and the total amount over that quarter.

Input Table(s)

merchants

ColumnTypeMeaning
merchant_idINTMerchant identifier. Primary key.
merchant_nameVARCHAR(100)Merchant name. NOT NULL, no uniqueness constraint.
categoryVARCHAR(50)Merchant category. May be NULL. Not needed for this task.
registration_dateDATEDate the merchant registered. May be NULL. Not needed for this task.

Key: merchant_id is the primary key.

transactions

ColumnTypeMeaning
transaction_idINTTransaction identifier. Primary key.
merchant_idINTMerchant the transaction belongs to. Foreign key to merchants.merchant_id.
amountDECIMAL(10,2)Transaction amount. NOT NULL and always positive.
transaction_dateDATEDate of the transaction. NOT NULL.

Keys: transaction_id is the primary key, and merchant_id references merchants.merchant_id.

Output Contract

Return one row per merchant that has at least one transaction dated in Q3 2024. Merchants with no Q3 2024 transactions do not appear.

ColumnMeaning
merchant_idThe merchant's id.
merchant_nameThe merchant's name.
avg_transaction_amountAverage amount of the merchant's Q3 2024 transactions, rounded to 2 decimal places.
transaction_countNumber of the merchant's Q3 2024 transactions.
total_amountSum of amount over the merchant's Q3 2024 transactions, rounded to 2 decimal places.

Order the rows by avg_transaction_amount descending (the rounded value shown in the output), then by merchant_id ascending. Because merchant_id is unique, this order is fully determined.

Example

merchants

merchant_idmerchant_namecategoryregistration_date
1Alpha BooksRetail2023-01-15
2Beta CafeFood2023-06-01
3Gamma TravelTravel2024-02-10
4Delta GamesGaming2024-05-20

transactions

transaction_idmerchant_idamounttransaction_date
101150.002024-07-01
102130.002024-08-15
1031200.002024-10-01
104240.002024-09-30
105235.002024-07-20
106245.002024-08-02
107325.502024-08-01
108310.252024-09-05
109312.002024-07-11
110499.992024-06-30

Expected output:

merchant_idmerchant_nameavg_transaction_amounttransaction_counttotal_amount
1Alpha Books40.00280.00
2Beta Cafe40.003120.00
3Gamma Travel15.92347.75

Transaction 103 falls on 2024-10-01 and transaction 110 on 2024-06-30, so both are outside the quarter, and merchant 4 has no Q3 transactions left. Merchants 1 and 2 both average 40.00, so merchant 1 comes first by merchant_id. Merchant 3 averages 47.75 / 3 = 15.9166..., which rounds to 15.92.

Constraints and Clarifications

  • merchants has at most 10,000 rows and transactions has at most 100,000 rows.
  • amount is never NULL and always positive. transaction_date is never NULL and always a valid date.
  • Every merchant_id in transactions references an existing merchant.
  • transaction_id is unique, so no transaction appears twice.
  • Two merchants can have the same merchant_name . They are still different merchants and get separate rows.
  • Ties on the rounded avg_transaction_amount are broken by merchant_id ascending.