How do you optimize database queries for performance?
Using the provided PostgreSQL tables, write an efficient query that calculates qualifying customer order totals for calendar year 2025. The query should include only completed orders, exclude incomplete customer or order relationships, and return customers whose total is at least 500.
Output
- One row per qualifying customer with
customer_id, customer_name, completed_order_count, and total_amount.
- Sort by
total_amount descending, then customer_id ascending.
Output details
- Calculate each order's total as the sum of
quantity * unit_price across its items.
- Count each qualifying order once in
completed_order_count.
- Return
completed_order_count as an integer and total_amount with numeric precision (12,2), rounding to two decimal places.