Explain the difference between a LATERAL JOIN and a correlated subquery. When would you use one over the other?

Quick Overview

Compare a LATERAL join with a correlated subquery in SQL: what each can return per outer row, where it can appear in a query, and which one fits a given task such as top-N per group. Tests advanced SQL reasoning about query shape, missing matches and per-row performance.

Explain the difference between a LATERAL JOIN and a correlated subquery. When would you use one over the other?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the difference between a `LATERAL` join and a correlated subquery. When would you use one over the other? ```hint Count rows and columns For each construct, ask how many rows and how many columns it is allowed to return per outer row, and in which clause of the query it can appear. ``` ```hint Try a concrete case Write "the three most recent orders per customer, with amount and date" both ways. Notice where one form gets awkward. ``` ### What a Strong Answer Covers - That both forms can reference columns of the current outer row, and the difference in what they return: one value for a scalar subquery, any number of rows and columns for `LATERAL`. - Where each one lives (`SELECT` or `WHERE` versus `FROM`), including `EXISTS` and `IN` subqueries as correlated filters. - What happens to an outer row with no match in each form. - Concrete cases for each: a single derived value or existence check versus top-N per group, several columns from one lookup, or a set-returning function. - Performance and portability: per-row execution, an index on the correlation key, and the `APPLY` equivalent in SQL Server. ### Follow-up Questions - How would you get the top 3 orders per customer with a window function instead, and when would you expect the `LATERAL` version to be faster? - How does PostgreSQL plan an `EXISTS` subquery compared with a scalar subquery in the `SELECT` list? - How would you expand a JSON array column into one row per element while keeping the parent row's columns? - What does a `LATERAL` subquery return when it contains an aggregate and no `GROUP BY`, and why does that matter for `CROSS JOIN LATERAL`?

Overview: Compare a LATERAL join with a correlated subquery in SQL: what each can return per outer row, where it can appear in a query, and which one fits a given task such as top-N per group. Tests advanced SQL reasoning about query shape, missing matches and per-row performance.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the difference between a LATERAL join and a correlated subquery. When would you use one over the other?

What a Strong Answer Covers Guidance

  • That both forms can reference columns of the current outer row, and the difference in what they return: one value for a scalar subquery, any number of rows and columns for LATERAL .
  • Where each one lives ( SELECT or WHERE versus FROM ), including EXISTS and IN subqueries as correlated filters.
  • What happens to an outer row with no match in each form.
  • Concrete cases for each: a single derived value or existence check versus top-N per group, several columns from one lookup, or a set-returning function.
  • Performance and portability: per-row execution, an index on the correlation key, and the APPLY equivalent in SQL Server.

Follow-up Questions Guidance

  • How would you get the top 3 orders per customer with a window function instead, and when would you expect the LATERAL version to be faster?
  • How does PostgreSQL plan an EXISTS subquery compared with a scalar subquery in the SELECT list?
  • How would you expand a JSON array column into one row per element while keeping the parent row's columns?
  • What does a LATERAL subquery return when it contains an aggregate and no GROUP BY , and why does that matter for CROSS JOIN LATERAL ?