Beginner

553. Nested Loop JOIN

Understand the Nested Loop JOIN algorithm and when database engines may use it.

📘 Lesson 553 of 728 ⏱ 14 min 🎯 Advanced

A Nested Loop JOIN processes rows from one input and searches for matching rows in the other input. It can be efficient when one input is small and the other side has an appropriate index.

Understanding JOIN algorithms helps explain why the same SQL query can perform differently depending on table size and indexes.

A small Customers result may be used to efficiently find matching Orders through an indexed CustomerID column.

SQL
SELECT
    c.CustomerName,
    o.OrderID
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID;

Common in query optimization and execution-plan analysis.

  • Nested Loop is a JOIN execution strategy.
  • It can work well when one input is small.
  • Indexes can make lookups efficient.
  • Large unindexed inputs can make it expensive.
  • The optimizer chooses the JOIN strategy.
What is a Nested Loop JOIN?

It is a JOIN algorithm that processes rows from one input and searches for matching rows in another input.

When can Nested Loop JOIN perform well?

It can perform well when one input is relatively small and the other input has an efficient index for the JOIN condition.

Have you finished this lesson?

Introduction to SQL – All Lessons