advanced
Complex SQL Query Refactoring
Optimize a poorly performing SQL query involving multiple joins and subqueries.
📝 プロンプトの内容
Given a hypothetical PostgreSQL schema with tables: orders (10M rows), customers (1M rows), and products (500k rows). The existing query uses five correlated subqueries and DISTINCT to calculate the lifetime value of customers who purchased electronics in the last quarter. Rewrite this query to use Common Table Expressions (CTEs) and window functions to maximize performance. Explain the execution plan differences.