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.
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 Tables
`merchants`
| Column | Type | Meaning |
|---|---|---|
| merchant_id | INTEGER | 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. |
`merchant_id` is the primary key of `merchants`.
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INTEGER | Transaction identifier. Primary key. |
| merchant_id | INTEGER | 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. |
`transaction_id` is the primary key of `transactions`, and `merchant_id` references `merchants.merchant_id`.
### Output
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.
### 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.
Tables
merchants(merchant_id INTEGER, merchant_name VARCHAR(100), category VARCHAR(50), registration_date DATE)
transactions(transaction_id INTEGER, merchant_id INTEGER, amount DECIMAL(10,2), transaction_date DATE)
Hints
- Check that transactions dated 2024-07-01 and 2024-09-30 are counted and that a transaction dated 2024-10-01 is not.
- Only merchants with at least one Q3 2024 transaction belong in the result.