Explain correlated subqueries and give an example

SQL
Hard
121.4K views

This tests understanding of subqueries dependent on outer rows. It evaluates advanced query logic skills.

Why Interviewers Ask This

Correlated subqueries are powerful but can be slow. Interviewers want to ensure candidates understand the execution model where the inner query runs for each outer row. It tests performance awareness.

How to Answer This Question

Define a correlated subquery as one referencing outer query columns. Explain it re-evaluates for each row. Provide a clear example, such as finding employees paid above their department average.

Key Points to Cover

  • Depends on outer query columns
  • Re-evaluated for every outer row
  • Can be performance intensive
  • Useful for row-by-row comparisons

Sample Answer

A correlated subquery depends on the current row of the outer query, referencing its columns and re-evaluating for each row. For example, to find employees paid above their department average, the subquery calculates the…

Common Mistakes to Avoid

  • Confusing with scalar subqueries
  • Ignoring performance implications
  • Writing inefficient nested loops

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 84 SQL questions