Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

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
SQL topics
Lesson 43/ 160
03. Joins & Relational Querying

INNER JOIN Mechanics & Multi-Table Joins

Return only matching rows and chain multiple INNER JOINs cleanly.

BeginnerINNER · ON · Match3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

INNER JOIN glues rows from two tables together when they share a key, and throws away rows that don't have a partner.

Think of it like…

Matching guests to tickets at a concert: only people who have a ticket and are on the guest list get in. Tickets with no guest and guests with no ticket are both turned away.

Where you’ve seen it

SELECT o.id, c.name, p.title FROM orders o JOIN customers c ON c.id = o.customer_id JOIN products p ON p.id = o.product_id builds an order report from three tables. Any order whose customer was deleted disappears from it.

Words you’ll hear

Join key
The columns compared in ON, usually a foreign key and the primary key it references.
Matched pair
One output row made from one row of each table that satisfies ON.
Fan-out
One row matching several rows on the other side, multiplying the output.
Table alias
A short name (o, c) used to say which table each column comes from.

Visual diagram

Loading interactive diagram

Theory & example queries

What INNER JOIN returns

Conceptually, the database pairs every row of the first table with every row of the second and keeps the pairs where ON is true. Real engines never build all those pairs; they use a nested loop, hash join or merge join, but the result is the same.

Because only matched pairs are kept, the result can be smaller than either table (rows without partners drop out) or larger (one row matching many). For a foreign key to a primary key, each child row matches at most one parent, so the result has at most as many rows as the child table.

Orders with customer namesQuery example
1Input dataStart from the source rows or state that the statement sees.
orders
4 rows
orders sample rows
idcustomer_idamount
50112499
5022899
50331750
5049310
customers
3 rows
customers sample rows
idname
1Asha Rao
2Kabir Shah
3Meera Sen
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1SELECT2  o.id,3  o.amount,4  c.name5FROM6  orders o7  INNER JOIN customers c ON c.id = o.customer_id;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Project and calculate

Calculate the selected expressions and return the requested output columns.

1/7
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative matched orders3 sample rows
Representative matched orders
idamountname
5012499Asha Rao
502899Kabir Shah
5031750Meera Sen

Note: Illustrative orders that each have a matching customer; orphaned orders would not appear in this INNER JOIN.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: INNER JOIN keeps only rows that have a partner on both sides.

Chaining several joins

Each additional JOIN combines the result so far with one more table. For inner joins the order you write them in doesn't change the result, and the optimizer picks its own order, so write them in the order that reads most clearly.

Qualify every column with a table alias once more than one table is involved. Otherwise a column like id or created_at is ambiguous, or worse, silently comes from the wrong table after someone adds a column later.

Three tables in one queryQuery example
1Input dataStart from the source rows or state that the statement sees.
orders
2 rows
orders sample rows
idcustomer_id
5011
5022
order_items
3 rows
order_items sample rows
order_idproduct_idqty
501111
501422
502771
products
3 rows
products sample rows
idtitle
11SQL Handbook
42Mechanical Keyboard
77USB-C Hub
2QuerySwitch dialects, then select a line or execution step to trace it.
query.sql
1SELECT2  c.name,3  p.title,4  oi.qty5FROM6  order_items oi7  JOIN orders o ON o.id = oi.order_id8  JOIN customers c ON c.id = o.customer_id9  JOIN products p ON p.id = oi.product_id;
Logical execution order
Native / baseline

PostgreSQL is the lesson baseline unless noted otherwise.

Line 1 · Project and calculate

Calculate the selected expressions and return the requested output columns.

1/9
3OutputInspect the representative rows, affected-row count, plan, or command status.
Representative purchase detail3 sample rows
Representative purchase detail
nametitleqty
Asha RaoSQL Handbook1
Asha RaoMechanical Keyboard2
Kabir ShahUSB-C Hub1

Note: Assumes each item joins to an order, customer and product. Rows with any missing link are excluded by the inner joins.

4ExplanationConnect the result back to the SQL rule and the edge case it demonstrates.

Key takeaway: Join order doesn't change an inner join's result; aliases prevent ambiguity.

Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Schema

Find the relationship

Identify the foreign key that links the tables, for example orders.customer_id referencing customers.id.

Focus at this step

orders.customer_id → customers.id

What to watch at this step
Usually
FK = PK
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

INNER JOIN combines rows from two tables wherever the ON condition is true, and returns only those matched pairs. A row with no partner on the other side is left out. Plain JOIN means INNER JOIN.

Why it matters: Almost every query that answers a business question joins tables, and most join bugs are about which rows silently drop out or get duplicated. Knowing exactly what INNER JOIN keeps lets you predict the row count before you run the query.

When to reach for it

  • Reports that combine facts with their descriptions (orders with customer and product names)
  • Lookups where a match is guaranteed by a foreign key constraint
  • Filtering to rows that have related data, when you also need that data's columns

When to avoid or be careful

  • INNER JOIN when unmatched rows should still appear in the report
  • Joining on columns that aren't keys, which can multiply rows unexpectedly
  • Old comma syntax (FROM a, b WHERE a.id = b.a_id): forgetting the condition creates a cross join

INNER JOIN vs LEFT JOIN vs EXISTS

INNER JOIN
Best for: Combining rows that must exist on both sides

Strengths: Simple results, no NULL padding.

Watch out: Silently drops rows without a match.

LEFT JOIN
Best for: Keeping every row of one table, matched or not

Strengths: Nothing disappears from the left table.

Watch out: Right-side columns are NULL for unmatched rows.

EXISTS
Best for: Filtering to rows that have a match, without needing the other table's columns

Strengths: Never duplicates rows, however many matches there are.

Watch out: You can't select columns from the subquery's table.

Interview signals & numbers to know

When this topic comes up

  • “Combine data from these two tables”
  • “Why did rows disappear after the join?”
  • “Why did my totals double?”
  • Foreign keys
  • Ambiguous column name

Benchmarks & rules of thumb

≤ child rows -
result size for FK → PK joins
0 -
rows kept that have no partner
JOIN -
means INNER JOIN

Common mistakes & follow-ups

Common interview mistakes

  • Using INNER JOIN and losing customers who have no orders from a report that should show zeros.
  • Joining on the wrong column (o.id = c.id instead of o.customer_id = c.id), which returns plausible-looking but wrong rows.
  • Leaving columns unqualified in a multi-table query and getting an "ambiguous column" error or the wrong table's column.

Likely interviewer follow-ups

  • What is the difference between INNER JOIN and LEFT JOIN? When would the results be identical?
  • Why might a join return more rows than the larger of the two tables?
  • Does the order of tables in a chain of INNER JOINs affect the result or the performance?
Interview tip: Before writing the query, say the expected row count out loud ("one row per order, orders without a customer drop out"). It shows you understand what the join keeps.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply INNER JOIN Mechanics & Multi-Table Joins with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Bridge / Junction Tables for Many-to-Many Relationships
Next topic
LEFT JOIN (Left Outer Join) & Preserving Unmatched Rows