Java Zero to All All questionsPractise free
Interview questions › Databases & Persistence › SQL Mastery & Joins

When would you use a correlated subquery versus a JOIN or a window function, and what's the performance concern?

Level III · MidSQL Mastery & Joins
Short answer

A correlated subquery references the outer row and conceptually runs once per outer row, which is intuitive for 'rows where X is the max for its group' but can be slow if the optimizer can't rewrite it.

Explanation

A JOIN (often to a derived/grouped table) is usually faster for set-based filtering, and window functions like ROW_NUMBER()/RANK() OVER (PARTITION BY ...) express top-N-per-group elegantly without a self-join. The practical guidance: prefer set-based JOINs or window functions for large data, and check the execution plan, because a naive correlated subquery can degrade to O(n*m).

Common mistakes
  • Using correlated subqueries on large tables without checking the plan.
  • Not considering a window function instead.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for SQL Mastery & Joins.

Practise free

More on SQL Mastery & Joins