beginner
INNER JOIN
6 min readLast updated: 2026-07-23
Overview
An INNER JOIN matches rows from two tables based on a shared key or condition, returning only records that exist in both tables.
Learning Objectives
- Connect tables using
INNER JOINand explicitONkey predicates. - Filter out non-matching records automatically.
- Combine fields across multiple tables in a single query.
Detailed Concept Explanation
INNER JOIN is the default join type in SQL. It evaluates the condition specified in the ON clause for each pair of rows:
- If the
ONcondition evaluates toTRUE, the combined row is included in the output. - If the
ONcondition evaluates toFALSEorUNKNOWN(e.g. key isNULL), the row is excluded.
Code Examples
SQL
sql
-- Retrieve matching customer order details
SELECT
c.customer_id,
c.name AS customer_name,
o.order_id,
o.total_amount
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id;
Best Practices
- Always Use Table Aliases: Use concise aliases (e.g.,
customers c,orders o) to improve query readability and prevent ambiguous column reference errors. - Explicit Join Conditions: Always specify explicit join predicates inside the
ONclause rather than putting join conditions insideWHERE.
Interview Perspective
Note
Interview Question: What happens when an INNER JOIN matches duplicate keys?
If Table A has 2 matching rows for a key and Table B has 3 matching rows for that same key, the INNER JOIN output will contain $2 \times 3 = 6$ combined rows for that key (a Cartesian product per key match).
Interactive Challenges
Summary
INNER JOIN combines rows from two tables where key conditions match in both tables, ignoring non-matching rows.