Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

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 Science topics
Lesson 24/ 160
02. SQL & Data Engineering Basics

INNER, LEFT, RIGHT & FULL Joins

Combine tables with join types and understand row multiplication and unmatched keys.

BeginnerINNER JOIN · LEFT JOIN3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A join lines up rows from two tables by a shared ID; the join type decides what happens to rows that don't find a partner.

Think of it like…

Attaching a name tag list to a guest list: with a LEFT join every guest stays on the list even if no tag exists (blank tag); with an INNER join guests without a tag are dropped; if a guest appears on the tag list twice, they appear twice on your result.

Where you’ve seen it

To predict loan default, a data scientist starts from loans (500,000 rows). She LEFT JOINs a customer-level table of credit-bureau features (one row per customer), and an account-activity summary (one row per loan, pre-aggregated). An early INNER JOIN to the bureau table dropped 90,000 loans from first-time borrowers with no bureau record, who default at twice the average rate; the LEFT JOIN keeps them with NULL features and adds has_bureau_record as a flag.

Words you’ll hear

INNER JOIN
Rows with a match in both tables.
LEFT JOIN
All left rows; right columns are NULL when no match.
FULL OUTER JOIN
All rows from both sides; useful for reconciling two sources.
Fan-out
A join that multiplies rows because the right side has several rows per key.

Visual diagram

Loading interactive diagram

Theory & deep dive

Choosing the join type

Use LEFT JOIN from the entity table to enrich it with attributes and features; it preserves the population. Use INNER JOIN only when missing matches should genuinely exclude rows (for example, restricting to customers who have a subscription), and then check how many rows you dropped. FULL OUTER JOIN is a reconciliation tool: WHERE a.id IS NULL OR b.id IS NULL finds records present in only one system. RIGHT JOIN is rare because swapping table order and using LEFT JOIN reads more clearly.

Mind where conditions go: a filter on the right table in WHERE after a LEFT JOIN turns it into an inner join (rows with NULLs fail the condition). Put right-table conditions in the ON clause (LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= :start) to keep unmatched left rows. Joins on NULL keys never match, so decide how to handle missing keys up front.

Customers with their orders in the window, keeping customers without any

SELECT c.customer_id, COUNT(o.order_id) AS orders FROM customers c LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.created_at >= :start GROUP BY c.customer_id; Customers with no orders get 0, because COUNT(o.order_id) ignores the NULL.

Key takeaway: LEFT JOIN from the entity table; right-table filters belong in ON.

Preventing fan-out and verifying joins

Before joining, verify the join key's uniqueness on the right side: SELECT customer_id, COUNT(*) FROM bureau GROUP BY 1 HAVING COUNT(*) > 1. If a table has many rows per key, aggregate it to one row per key first (latest record with ROW_NUMBER, or SUM/COUNT features), or join through a time-aware condition so you only pick the row valid at the prediction date. After the join, assert the row count equals the base table's, and measure the unmatched rate with a COUNT of NULL right keys.

Time matters for modelling: joining a customer's *current* attributes to historical training rows leaks the future. Use as-of joins (the latest feature row with valid_from <= prediction_date) or snapshot tables. In pandas the same checks apply: merge(..., validate="many_to_one", indicator=True), and pd.merge_asof for time-based matches. Document each join's assumption ("bureau is one row per customer, latest pull before application").

A safe join check in pandas

out = loans.merge(bureau, on="customer_id", how="left", validate="many_to_one"); assert len(out) == len(loans) raises immediately if bureau has duplicate customers or the row count changed.

Key takeaway: Check key uniqueness, assert row counts, and join as-of in time to avoid leakage.

How it works, step by step

Step 1 of 4
Design

1. Choose the base table

Pick the table that defines one row per modelling example and keep it on the left.

FROM loans
Key numbers at this step
Population
fixed

When to use it & trade-offs

What it is

Joins combine tables on matching keys. INNER JOIN keeps only matches; LEFT JOIN keeps every row from the left table, filling NULLs where there's no match; RIGHT JOIN is the mirror image; FULL OUTER JOIN keeps unmatched rows from both. For data scientists the usual pattern is: start from the table that defines your modelling grain (customers, loans, sessions), LEFT JOIN features onto it, and make sure each joined table has at most one row per key, so row counts don't change. The pandas equivalent is df.merge(other, on=key, how=...).

Why it matters: Join mistakes silently corrupt training data: an INNER JOIN drops customers with no history (usually the ones you most need to predict for), a many-to-many join multiplies rows and weights some customers heavily, and a wrong join key attaches features to the wrong entity. Validating row counts after every join is a basic safeguard, and it's a popular interview topic.

When to reach for it

  • Building modelling tables from several sources
  • Reconciling counts between systems
  • Finding entities with no activity

When to avoid or be careful

  • INNER JOIN on optional feature tables
  • Joining current attributes to historical rows
  • Many-to-many joins into a modelling table

Join types

INNER JOIN
Best for: Restricting to entities that exist in both tables

Strengths: Smallest result; simple.

Watch out: Silently drops unmatched rows.

LEFT JOIN
Best for: Enriching a base population with features

Strengths: Keeps every base row.

Watch out: NULLs to handle; fan-out if the right side isn't unique.

FULL OUTER JOIN
Best for: Reconciling two sources

Strengths: Shows what's missing on either side.

Watch out: Rarely needed for modelling tables.

Interview signals & numbers to know

When this topic comes up

  • “Row count changed after the join”
  • “LEFT vs INNER JOIN”
  • “Customers with no history are missing”
  • As-of joins
  • Join validation

Benchmarks & rules of thumb

500,000 -
loan rows that must survive every join in the example
90,000 -
borrowers dropped by the INNER JOIN mistake
validate= -
pandas safeguard against fan-out

Common mistakes & follow-ups

Common interview mistakes

  • Using an INNER JOIN to a bureau table and dropping first-time borrowers.
  • Putting WHERE o.created_at >= :start after a LEFT JOIN and removing customers with no orders.
  • Joining today's customer segment to last year's loans and leaking future information.

Likely interviewer follow-ups

  • Why is an inner join risky for a modelling table?
  • How do you avoid duplicating rows in a join?
  • How do you join features without leaking the future?
Interview tip: State the base table and the grain first, then explain each join's assumption. That's how experienced data scientists talk about joins.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply INNER, LEFT, RIGHT & FULL Joins with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Aggregate Functions & GROUP BY / HAVING
Next topic
Self Joins & Cross Joins