You are asked how you would optimize a slow-running PostgreSQL query used in an Automation Anywhere reporting or operational workflow. Describe how you would diagnose the issue, interpret the execution plan, and decide whether the main problem is in joins, subqueries, CTEs, window functions, or aggregations.
Reading `EXPLAIN (ANALYZE, BUFFERS)` outputReasoning about join selectivity and access pathsKnowing when to rewrite subqueries or CTEsUnderstanding the cost of window functions and aggregationsBalancing SQL rewrites with indexing trade-offsA strong answer should be practical and PostgreSQL-specific. The interviewer expects more than “add an index”: explain how you would measure the problem, isolate the bottleneck, propose a rewrite or indexing strategy, and validate that the optimized query still returns correct results.