What is a LATERAL join (CROSS APPLY / OUTER APPLY), and when would you use it for top-N-per-group queries?

Quick Overview

Asks what a LATERAL join (CROSS APPLY and OUTER APPLY in SQL Server) does and when to reach for it. Tests understanding of correlated subqueries in the FROM clause, top-N-per-group queries, inner versus outer semantics, and how the approach compares with window functions.

What is a LATERAL join (CROSS APPLY / OUTER APPLY), and when would you use it for top-N-per-group queries?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain what a `LATERAL JOIN` (or `CROSS APPLY`/`OUTER APPLY`) is. When would you use it, particularly for correlated per-row subqueries or top-N-per-group problems? ```hint Scoping in the FROM clause Think about what a derived table in `FROM` is normally allowed to reference, and what becomes possible if it could see the current row of a table listed before it. ``` ### What a Strong Answer Covers - What `LATERAL` changes about scoping in the `FROM` clause, and how `CROSS APPLY` and `OUTER APPLY` map to it - A correct top-N-per-group query with deterministic ordering - The difference between the inner and outer forms, and what happens to rows with no match - When the approach beats a window-function query and when it does not, including the role of indexes - Other uses, such as set-returning functions or multi-column correlated lookups ### Follow-up Questions - How would you write the same top-3-orders query with a window function, and when would you pick that version? - What index makes the `LATERAL` version fast, and what does the query plan look like with it? - A `LEFT JOIN LATERAL` query starts losing customers with no orders after you add a `WHERE` clause. Why? - How would you return the latest order per customer in a database with no `LATERAL` or `APPLY` support?

Overview: Asks what a LATERAL join (CROSS APPLY and OUTER APPLY in SQL Server) does and when to reach for it. Tests understanding of correlated subqueries in the FROM clause, top-N-per-group queries, inner versus outer semantics, and how the approach compares with window functions.

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

Explain what a LATERAL JOIN (or CROSS APPLY/OUTER APPLY) is. When would you use it, particularly for correlated per-row subqueries or top-N-per-group problems?

What a Strong Answer Covers Guidance

  • What LATERAL changes about scoping in the FROM clause, and how CROSS APPLY and OUTER APPLY map to it
  • A correct top-N-per-group query with deterministic ordering
  • The difference between the inner and outer forms, and what happens to rows with no match
  • When the approach beats a window-function query and when it does not, including the role of indexes
  • Other uses, such as set-returning functions or multi-column correlated lookups

Follow-up Questions Guidance

  • How would you write the same top-3-orders query with a window function, and when would you pick that version?
  • What index makes the LATERAL version fast, and what does the query plan look like with it?
  • A LEFT JOIN LATERAL query starts losing customers with no orders after you add a WHERE clause. Why?
  • How would you return the latest order per customer in a database with no LATERAL or APPLY support?