Lateral Joins

SQL · Extra Concepts

Theory

A normal subquery in the FROM clause cannot see the other tables in that FROM clause. A LATERAL join removes that limit: the subquery runs once for each row of the table on its left and can use that row's columns. Think of it as a for-each loop: for each department d, run 'SELECT the highest-paid employee WHERE dept_id = d.id ORDER BY salary DESC LIMIT 1'.

This makes LATERAL the cleanest way to write top-N-per-group queries (top 3 orders per customer, latest login per user) and to call table-returning functions per row. PostgreSQL, MySQL, Oracle and Snowflake use the LATERAL keyword; SQL Server and Oracle also offer CROSS APPLY (like an inner lateral join) and OUTER APPLY (keeps left rows with no match, like a LEFT JOIN). SQLite has no LATERAL, but for a single value per row a correlated scalar subquery in the SELECT list gives the same result. For larger N, window functions with ROW_NUMBER() are the portable alternative.

Why we use it

Questions like 'top 2 orders for each customer' need a per-row subquery that can use ORDER BY and LIMIT. LATERAL lets that subquery reference the outer row, which a regular derived table cannot do.

When to use it

Use LATERAL or CROSS APPLY for top-N per group and per-row function calls. In databases without it, use ROW_NUMBER() OVER (PARTITION BY ...) and filter rn <= N, or a correlated subquery for a single value.

How it works

  1. Take the next row from the left table (a department)
  2. Run the lateral subquery using that row's columns
  3. Subquery sorts and limits: top earner for this department
  4. Subquery returned rows? Yes: Join them to the row. No: Drop (CROSS) or NULLs (LEFT/OUTER).
  5. More left rows? Yes: Repeat for the next department. No: Finish.
  6. Return the combined result

Syntax

FROM t1, LATERAL (
  SELECT ... FROM t2 WHERE t2.col = t1.col
) sub

Example

SELECT d.name, top.name AS top_earner
FROM departments d,
LATERAL (
  SELECT name FROM employees e WHERE e.department_id = d.id ORDER BY salary DESC LIMIT 1
) top;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic