Correlated vs. non-correlated subqueries: differences, performance, and when to use each in analytical SQL

Quick Overview

Asks you to explain how correlated and non-correlated SQL subqueries differ and what each costs at query time. Tests understanding of per-row evaluation, optimizer rewrites into semi-joins, window function alternatives, and which form to choose for existence checks, thresholds and per-group metrics.

Correlated vs. non-correlated subqueries: differences, performance, and when to use each in analytical SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the difference between a correlated and non-correlated subquery. Discuss performance implications and when you would prefer one in complex analytical scenarios. ```hint Could it run on its own? Ask whether the inner query would still run if you copied it out of the outer query. If it would not, think about how many times the engine might have to evaluate it. ``` ```hint Count the evaluations For a comparison against a per-group metric over N rows, count how many times the inner aggregate is evaluated, and compare that with how many distinct values it can take. ``` ### What a Strong Answer Covers - A definition of each kind and how to tell them apart from the SQL text. - How each is executed, both naively and after the optimizer rewrites it. - When a correlated subquery becomes slow, and the rewrites that fix it. - Which form fits existence tests, global thresholds, and per-group metrics. - How to confirm what the engine actually does with a given query. ### Follow-up Questions - Why can `NOT IN` with a subquery return no rows at all, and what should you write instead? - Rewrite a correlated "price above its category average" query using a window function. - How do you read an execution plan to tell whether a correlated subquery was turned into a join? - When would you use a `LATERAL` join instead of a correlated subquery?

Overview: Asks you to explain how correlated and non-correlated SQL subqueries differ and what each costs at query time. Tests understanding of per-row evaluation, optimizer rewrites into semi-joins, window function alternatives, and which form to choose for existence checks, thresholds and per-group metrics.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the difference between a correlated and non-correlated subquery. Discuss performance implications and when you would prefer one in complex analytical scenarios.

What a Strong Answer Covers Guidance

  • A definition of each kind and how to tell them apart from the SQL text.
  • How each is executed, both naively and after the optimizer rewrites it.
  • When a correlated subquery becomes slow, and the rewrites that fix it.
  • Which form fits existence tests, global thresholds, and per-group metrics.
  • How to confirm what the engine actually does with a given query.

Follow-up Questions Guidance

  • Why can NOT IN with a subquery return no rows at all, and what should you write instead?
  • Rewrite a correlated "price above its category average" query using a window function.
  • How do you read an execution plan to tell whether a correlated subquery was turned into a join?
  • When would you use a LATERAL join instead of a correlated subquery?