Per-merchant statistics for completed 2024 transactions with an index-friendly query
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A merchant report over a large `transactions` table runs slowly. Write an efficient query that returns per-merchant statistics for completed transactions in 2024: `merchant_id`, `merchant_name`, `category`, `total_transactions` (count), `total_amount` (sum), `avg_amount` (average), `first_transaction` (earliest date) and `last_transaction` (latest date).
`transactions` can hold up to 1,000,000 rows, so write the filters in a form an index can use, and be ready to say which index you would add.
```hint Filtering on a date
Compare the bare `transaction_date` column against constants. Wrapping the column in a function keeps the planner from using an index on it.
```
### Input Table(s)
`merchants`
| Column | Type | Meaning |
|---|---|---|
| merchant_id | INT | The merchant |
| merchant_name | VARCHAR(100) | Merchant name, not null |
| category | VARCHAR(50) | Merchant category, not null |
Primary key: `merchant_id`.
`transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INT | One row per transaction |
| merchant_id | INT | Merchant that took the payment, not null, references `merchants.merchant_id` |
| amount | DECIMAL(12,2) | Transaction amount, not null |
| status | VARCHAR(10) | `'completed'`, `'pending'`, `'failed'` or `'refunded'`, not null |
| transaction_date | DATE | Day of the transaction, not null |
Primary key: `transaction_id`.
### Output Contract
| Column | Meaning |
|---|---|
| merchant_id | The merchant |
| merchant_name | From `merchants` |
| category | From `merchants` |
| total_transactions | Number of qualifying transactions |
| total_amount | Sum of `amount` over qualifying transactions, rounded to 2 decimal places |
| avg_amount | Average `amount` over qualifying transactions, rounded to 2 decimal places |
| first_transaction | Earliest qualifying `transaction_date` |
| last_transaction | Latest qualifying `transaction_date` |
A qualifying transaction has `status = 'completed'` and a `transaction_date` from `'2024-01-01'` (inclusive) to `'2025-01-01'` (exclusive). Return one row per merchant with at least one qualifying transaction, ordered by `total_amount` descending, then `merchant_id` ascending.
### Example
`merchants`
| merchant_id | merchant_name | category |
|---|---|---|
| 1 | Corner Cafe | Food |
| 2 | Bolt Electronics | Electronics |
| 3 | Page Books | Books |
| 4 | Fresh Mart | Grocery |
`transactions`
| transaction_id | merchant_id | amount | status | transaction_date |
|---|---|---|---|---|
| 101 | 1 | 12.50 | completed | 2024-01-05 |
| 102 | 1 | 7.25 | completed | 2024-03-18 |
| 103 | 1 | 30.00 | refunded | 2024-04-02 |
| 104 | 2 | 199.99 | completed | 2024-12-31 |
| 105 | 2 | 49.00 | completed | 2025-01-01 |
| 106 | 3 | 15.00 | pending | 2024-06-10 |
| 107 | 3 | 20.00 | completed | 2023-12-31 |
| 108 | 4 | 10.00 | completed | 2024-02-14 |
| 109 | 4 | 5.00 | completed | 2024-02-20 |
| 110 | 4 | 4.75 | completed | 2024-07-01 |
| 111 | 4 | 0.25 | failed | 2024-07-02 |
Expected result:
| merchant_id | merchant_name | category | total_transactions | total_amount | avg_amount | first_transaction | last_transaction |
|---|---|---|---|---|---|---|---|
| 2 | Bolt Electronics | Electronics | 1 | 199.99 | 199.99 | 2024-12-31 | 2024-12-31 |
| 1 | Corner Cafe | Food | 2 | 19.75 | 9.88 | 2024-01-05 | 2024-03-18 |
| 4 | Fresh Mart | Grocery | 3 | 19.75 | 6.58 | 2024-02-14 | 2024-07-01 |
Merchant 2 keeps its 2024-12-31 sale and loses the one on 2025-01-01. Merchant 1's refunded row is dropped, and its average 9.875 rounds to 9.88. Merchant 4 averages 19.75 over 3 rows, which is 6.58 after rounding. Merchants 1 and 4 tie on 19.75, so `merchant_id` puts 1 first. Merchant 3 has only a pending 2024 row and a 2023 sale, so it does not appear.
### Constraints and Clarifications
- `merchants` has at most 10,000 rows and `transactions` at most 1,000,000.
- `status` is always one of `'completed'`, `'pending'`, `'failed'` or `'refunded'`. Only `'completed'` counts.
- No column holds NULL, and every `transactions.merchant_id` exists in `merchants`.
- `transaction_id` is unique, so each transaction is counted at most once.
- Merchants with no qualifying transaction do not appear, even if they have other transactions.
- Round halves away from zero, as PostgreSQL's `ROUND` does on `NUMERIC` values.
- Ties on `total_amount` are broken by `merchant_id` ascending.
Overview: Write an efficient PostgreSQL query that reports per-merchant counts, totals, averages and first and last dates for completed 2024 transactions in a table of up to a million rows. It tests grouped aggregation, index-friendly date ranges, tie-breaking order and choosing an index that serves the filter.