What is a correlated subquery and why can it be a performance bottleneck?
Last updated
A correlated subquery references a column from the outer query, so it is logically re-run for every outer row.
This acts like a nested loop: N outer rows can mean N inner scans, especially without an index on the correlated column.
Fix it by rewriting as a
JOINor pre-aggregated derived table, and by indexing the correlated column (many optimizers also decorrelate automatically).
- https://learn.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-ver17
- https://www.geeksforgeeks.org/sql/difference-between-nested-subquery-correlated-subquery-and-join-operation/
- https://www.geeksforgeeks.org/sql/sql-interview-questions/
- https://dev.to/rebooter_s/mysql-count-scalar-subquery-optimization-the-complete-guide-45e8
- https://medium.com/@jholt1055/database-query-optimization-the-complete-dba-guide-to-identifying-and-fixing-slow-queries-in-2025-80cf25c1c7bb
- https://medium.com/@anjalimishraa17/top-50-sql-database-interview-questions-the-complete-deep-dive-c5a6c9c1d728
- https://www.geeksforgeeks.org/data-science/sql-question-for-data-science-interview/
- https://www.intervue.io/interview-questions/sql-interview-questions/