How do you optimize a slow-running SQL query at Citi?
Using the accounts and transactions tables, write an optimized PostgreSQL query that returns active accounts whose completed transactions during Q1 2025 total at least 1,000. The query should avoid unnecessary rows before aggregation and produce a deterministic result.
account_id, account_name, q1_completed_amount, completed_transaction_countq1_completed_amount descending, then account_id ascending| Column | Type | Description |
|---|---|---|
| account_idPK | INT | Unique account identifier |
| account_name | VARCHAR(100) | Account holder name |
| account_status | VARCHAR(20) | Current account status |
| account_type | VARCHAR(30) | Account product type |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| account_id | INT | Related account identifier |
| transaction_date | DATE | Transaction posting date |
| status | VARCHAR(20) | Transaction processing status |
| amount | DECIMAL(12,2) | Transaction amount |