Your question is SQL Query Optimization. Start with the requirements and the two tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
How do you optimize a slow-running SQL query?
Using the provided schema, write an efficient PostgreSQL query that returns active customers and their completed transaction totals for January 2025. Preserve customers without matching transactions and avoid changing the required result.
customer_id, customer_name, transaction_count, and total_amounttotal_amount descending, then customer_id ascending| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer display name |
| is_active | BOOLEAN | Whether the customer is active |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| customer_id | INT | Customer associated with the transaction |
| transaction_status | VARCHAR(20) | Processing status of the transaction |
| transaction_date | TIMESTAMP | Timestamp when the transaction occurred |
| amount | DECIMAL(12,2) | Transaction amount |