Home/Learn/SQL/INNER JOIN

INNER JOIN

Beginner
Joins

INNER JOIN returns only rows where the join condition matches in both tables, excluding unmatched rows from either side.

Overview

INNER JOIN is the most common join type. It produces the intersection of the two tables based on the join predicate. The query planner can implement an inner join via nested loops (small tables), hash join (large unsorted tables), or merge join (pre-sorted inputs) — the choice depends on table sizes and available indexes. Implicit join syntax (comma-separated tables with WHERE) produces the same result but is harder to read and error-prone when WHERE conditions are accidentally omitted. Always use explicit JOIN ... ON syntax. Every FK column in a child table should have an index to support efficient join lookup.

Explicit vs Implicit Join Syntax

Both produce identical results but explicit JOIN syntax makes the join condition obvious and prevents accidental Cartesian products when WHERE conditions are missed.

SQL — explicit JOIN vs implicit comma join
-- Implicit (old-style, avoid): comma join with WHERE
SELECT o.id, u.email, o.total_amount
FROM orders o, users u
WHERE o.user_id = u.id    -- this IS the join condition
  AND o.status = 'pending';

-- Explicit (preferred): JOIN ... ON
SELECT o.id, u.email, o.total_amount
FROM orders o
INNER JOIN users u ON u.id = o.user_id
WHERE o.status = 'pending';

-- Multi-column join condition
SELECT oi.order_id, p.name, oi.quantity
FROM order_items oi
INNER JOIN products p ON p.id = oi.product_id
                      AND p.is_active = TRUE;   -- extra filter in ON clause

-- Self-describing aliases matter
SELECT
    o.id          AS order_id,
    o.total_amount,
    u.email       AS customer_email,
    u.username    AS customer_name
FROM orders o
INNER JOIN users u ON u.id = o.user_id;

JOIN Performance and Indexes

The join column on the inner (probed) table must be indexed for the planner to use an index nested-loop join. Without an index the planner typically falls back to a hash join, which consumes memory proportional to the inner table size.

SQL — index-backed INNER JOIN and EXPLAIN output
-- Index the FK column on the child table (join side that is probed)
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_order_items_order_id ON order_items(order_id);

-- EXPLAIN: verify the join algorithm chosen
EXPLAIN (ANALYZE, BUFFERS)
SELECT o.id, u.email, oi.quantity
FROM orders o
INNER JOIN users u        ON u.id = o.user_id
INNER JOIN order_items oi ON oi.order_id = o.id
WHERE o.status = 'completed'
  AND o.created_at >= NOW() - INTERVAL '7 days';

-- Typical EXPLAIN output hints:
-- "Index Scan using idx_orders_user_id"  → FK index used, good
-- "Hash Join" + "Seq Scan on users"      → users table being hashed (check size)
-- "Nested Loop" + "Index Scan"           → efficient for small row counts

-- JPA: INNER JOIN via @ManyToOne relationship (FK-based)
// JPQL: SELECT o FROM Order o JOIN o.user u WHERE o.status = 'completed'
// Spring Data: @Query("SELECT o FROM Order o JOIN FETCH o.user WHERE o.status = :s")

Key Points to Remember

  • 1INNER JOIN returns only rows with matching values in both tables.
  • 2Always use explicit JOIN ... ON syntax; avoid implicit comma joins.
  • 3Index the FK column on the child table to enable fast index nested-loop joins.
  • 4Extra filter conditions can go in the ON clause or the WHERE clause for INNER JOIN — semantically identical.
  • 5Join algorithms: nested loop (small), hash join (large unsorted), merge join (sorted inputs).
  • 6In JPA, use JOIN FETCH to load associations in a single query and avoid N+1 selects.

Interview Questions

Sign in to ask Aria
1

What is the difference between putting a filter in ON vs WHERE for an INNER JOIN?

MediumAmazon
2

Describe the three join algorithms (nested loop, hash, merge) and when each is chosen.

HardGoogle
3

Why should foreign key columns always be indexed?

MediumFlipkart
4

What is N+1 query problem in JPA and how do you solve it with JOIN FETCH?

HardNetflix

Ask Aria about INNER JOIN

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…