Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 23/ 160
02. Advanced SQL & Relational Databases

Complex Joins (INNER, LEFT, FULL, CROSS, LATERAL)

Combine tables correctly with join types including LATERAL for per-row correlated subqueries.

IntermediateINNER/LEFT · FULL/CROSS · LATERAL3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Define one output row before joining. Next, prove uniqueness on the matching side. Finally, compare input and output row counts.

Think of it like…

A join is a seating plan: INNER seats only matched pairs, LEFT keeps unmatched guests on the left, FULL keeps every guest, and LATERAL asks each guest for their personalized list.

Where you’ve seen it

FROM orders o LEFT JOIN LATERAL (SELECT * FROM payments p WHERE p.order_id=o.id ORDER BY paid_at DESC LIMIT 1) p ON true retrieves exactly one latest payment per order.

Words you’ll hear

INNER/LEFT
Joins that match keys and, for outer variants, preserve all rows on one side or both sides when no partner exists.
FULL/CROSS
A Cartesian or explicitly filtered full pairing of both inputs whose expected cardinality must be understood.
LATERAL
A correlated subquery on the right side of a join that can refer to each current left-hand row.
Operational boundary
A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation. Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

Visual diagram

Loading interactive diagram

Theory & deep dive

INNER/LEFT: the working mechanism

Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row.

Define one output row before joining. Prove uniqueness on the matching side, then INNER, LEFT, FULL, or LATERAL; this exposes the state governed by LATERAL.

Complex Joins in one concrete design

FROM orders o LEFT JOIN LATERAL (SELECT * FROM payments p WHERE p.order_id=o.id ORDER BY paid_at DESC LIMIT 1) p ON true retrieves exactly one latest payment per order.

Key takeaway: Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row

Complex Joins: failure mode and production response

A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.

Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained. Track Fanout as "output fact count divided by pre-join fact count", and make One-to-many visible to the dataset owner.

Key takeaway: Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

How it works, step by step

Step 1 of 4
INNER/LEFT

1. State grain

Define one output row before joining. Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row.

define one output row before joining
Key numbers at this step
Fanout
output fact count divided by pre-join fact count
FULL/CROSS
A Cartesian or explicitly filtered full pairing of both inputs whose expected cardinality must be understood.

When to use it & trade-offs

What it is

Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row. FROM orders o LEFT JOIN LATERAL (SELECT * FROM payments p WHERE p.order_id=o.id ORDER BY paid_at DESC LIMIT 1) p ON true retrieves exactly one latest payment per order.

Why it matters: A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation. Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

When to reach for it

  • Combine tables correctly with join types including LATERAL for per-row correlated subqueries.
  • Choose INNER/LEFT because it provides this capability: Joins that match keys and, for outer variants, preserve all rows on one side or both sides when no partner exists.
  • Keep evidence for Fanout at "output fact count divided by pre-join fact count" so the last production stage-Guard cardinality - compare input and output row counts-remains testable.

When to avoid or be careful

  • A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.
  • Do not conflate FULL/CROSS with INNER/LEFT. The second concept contributes different behavior: A Cartesian or explicitly filtered full pairing of both inputs whose expected cardinality must be understood.
  • Release only after this guardrail is in place: Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

INNER/LEFT / FULL/CROSS / LATERAL - roles, limits, and operating cost

INNER/LEFT
Best for: Joins that match keys and, for outer variants, preserve all rows on one side or both sides when no partner exists.

Strengths: Joins that match keys and, for outer variants, preserve all rows on one side or both sides when no partner exists. Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row.

Watch out: A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.

FULL/CROSS
Best for: A Cartesian or explicitly filtered full pairing of both inputs whose expected cardinality must be understood.

Strengths: A Cartesian or explicitly filtered full pairing of both inputs whose expected cardinality must be understood. In this workflow it supports: Choose preservation - INNER, LEFT, FULL, or LATERAL.

Watch out: The operating cost is real: a dimension key must not duplicate facts accidentally.

LATERAL
Best for: A correlated subquery on the right side of a join that can refer to each current left-hand row.

Strengths: A correlated subquery on the right side of a join that can refer to each current left-hand row. Its output can be checked with LIMIT in LATERAL.

Watch out: Operating guardrail: Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for INNER/LEFT, and how would Fanout reveal it?”
  • “Why is FULL/CROSS distinct from this behavior: a correlated subquery on the right side of a join that can refer to each current left-hand row?”
  • Production clue: A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.
  • Design probe: Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

Benchmarks & rules of thumb

Fanout -
output fact count divided by pre-join fact count
One-to-many -
a dimension key must not duplicate facts accidentally
LIMIT in LATERAL -
bounds the selected correlated rows per left row

Common mistakes & follow-ups

Common interview mistakes

  • A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.
  • Reporting success from a job exit code without checking Fanout and One-to-many.
  • Treating this example as complete without replay, rollback, or access boundaries: FROM orders o LEFT JOIN LATERAL (SELECT * FROM payments p WHERE p.order_id=o.id ORDER BY paid_at DESC LIMIT 1) p ON true retrieves exactly one latest payment per order.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Joins that match keys and, for outer variants, preserve all rows on one side or both sides when no partner exists.
  • What does the recovery plan do when this occurs? A one-to-many dimension join can multiply a fact's measure, a filter in WHERE can turn a LEFT join into INNER, and unbounded CROSS joins can explode cardinality before aggregation.
  • When is INNER/LEFT insufficient for One-to-many, and what does adding FULL/CROSS cost in Fanout?
Interview tip: Lead with the data path and failure boundary: Join semantics are defined by matching predicates and row preservation: INNER drops nonmatches, LEFT/FULL preserve sides, CROSS forms the Cartesian product, and LATERAL evaluates a right-side subquery for each left row. Then make the answer operational: Declare the join grain, inspect key uniqueness, place unmatched-row tests in ON rather than WHERE, and use aggregates or a LATERAL subquery only after cardinality is constrained.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Complex Joins (INNER, LEFT, FULL, CROSS, LATERAL) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
PostgreSQL Architecture, Schemas, Data Types & Constraints
Next topic
Common Table Expressions (CTEs) & Recursive CTEs