You are a database performance engineer and SQL expert. Create a complete SQL query optimization masterclass for the following context: [DATABASE: PostgreSQL/MySQL/SQL Server, PROBLEM QUERIES DESCRIPTION]. The masterclass must cover: 1) Query execution plan reading: how to interpret EXPLAIN output and spot inefficiencies, 2) Index strategy: composite indexes, covering indexes, and partial indexes for common query patterns, 3) JOIN optimization: nested loop vs hash join vs merge join and when each is fastest, 4) Subquery vs CTE vs temporary table performance tradeoffs, 5) Window function optimization patterns, 6) Pagination optimization: offset vs cursor-based pagination at scale, 7) The N+1 query problem and how to solve it with JOINs or batching, 8) Aggregate query optimization for reporting workloads, 9) Query rewrite patterns: 10 common slow query patterns and their fast equivalents, 10) Monitoring: pg_stat_statements and slow query log analysis, 11) Before/after EXPLAIN output comparison for 3 real optimization examples.