Learn SQL
-- joining.lateral_range_join

LATERAL & Range Joins in SQL

Per-row subqueries and band lookups

--concept

LATERAL (PG) runs a per-row subquery — each left row gets its own LIMITed lookup (top-N per group, index-friendly). Range/band joins use inequality ON (salary BETWEEN lo AND hi; event BETWEEN start AND end-effective) for pay grades and point-in-time versioning. Correct but heavier (nested-loop-ish); composite/covering indexes on range columns help. Half-open ends keep effective-dating exact.

--how it works
1

LATERAL per row

JOIN LATERAL (… LIMIT N) — each left row runs its own indexed lookup. Top-N per group.

2

Range ON bands

salary BETWEEN lo AND hi; event >= start AND < end — non-equi matching.

3

Half-open effective

Point-in-time correctness lives on the exclusive end.

4

Index the probe

Composite/covering on range cols; LATERAL wants inner ORDER + LIMIT index.

--visualization
nameband
AliceL4
BobL2
Stage 1 / 1
Non-equi ON: salary ranges, effective half-open ends.
--example
query.sql
1-- salary band lookup:
2SELECT e.name, b.band FROM employees e JOIN pay_bands b ON e.salary BETWEEN b.lo AND b.hi LIMIT 2;
Result
nameband
AliceL4
BobL2

Each salary probes the band table by range, not equality. LATERAL generalizes this to per-employee latest-orders lookups with LIMIT inside — impossible in plain JOIN.

--variants

Pay bands by range

No equality key — range probe.

query.sql
1SELECT e.name, b.band FROM employees e JOIN pay_bands b ON e.salary BETWEEN b.lo AND b.hi LIMIT 2;
Result
nameband
AliceL4
BobL2
1Each salary falls into its band interval.
2Nested-loop-ish but correct; indexes narrow it.
3Equality-model thinkers miss this shape.

Recent orders per customer (PG)

Per-customer LIMIT impossible in plain JOIN.

query.sql
1SELECT c.name, r.id FROM customers c JOIN LATERAL (SELECT id FROM orders o WHERE o.customer_id = c.id ORDER BY created_at DESC LIMIT 2) r ON true;
Result
customerorder_id
Ada501
Ada498
1Inner sees outer c.id per row — correlated deliberately.
2LIMIT 2 applies per customer, not globally.
3Fallback: ROW_NUMBER partition-filter.
--key facts
LATERALper-row LIMITed lookup — top-N per group
Range ONbands + effective dating
Half-openpoint-in-time correctness
--use cases
Top-N per group with per-row LIMITs
Versioned/effective-dated lookups
--under the hood
LATERAL inner must be index-backed (customer_id, created_at DESC) or it scans per outer row.
Range joins favor GiST/SP-GiST for interval overlap at scale.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.