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 JOIN and explicit ON key 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 ON condition evaluates to TRUE, the combined row is included in the output.
  • If the ON condition evaluates to FALSE or UNKNOWN (e.g. key is NULL), 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 ON clause rather than putting join conditions inside WHERE.

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

Challenge 1: Join Employees and Departments

Select employee 'name' and department 'dept_name' by joining 'employees' (e) and 'departments' (d) on 'department_id'.


Summary

INNER JOIN combines rows from two tables where key conditions match in both tables, ignoring non-matching rows.