Beginner

577. ANTI JOIN

Learn the concept of an Anti JOIN for finding records without a matching record in another table.

📘 Lesson 577 of 728 ⏱ 13 min 🎯 Advanced

An Anti JOIN returns rows from one table that do not have a corresponding match in another table. SQL commonly implements this pattern using NOT EXISTS or LEFT JOIN with IS NULL.

Finding missing relationships is important for exception reports, reconciliation, and data-quality analysis.

Find customers who have no orders.

SQL
SELECT
    c.CustomerID,
    c.CustomerName
FROM Customers c
WHERE NOT EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.CustomerID = c.CustomerID
);

Used in customer analysis, reconciliation, compliance checks, and data-quality reporting.

  • Anti JOIN finds unmatched records.
  • NOT EXISTS is a common implementation.
  • LEFT JOIN with IS NULL is another approach.
  • It is useful for exception reporting.
  • It can be safer than NOT IN when NULLs are involved.
What is an Anti JOIN?

An Anti JOIN returns records from one table that have no matching records in another table.

How can Anti JOIN be implemented?

Common approaches include NOT EXISTS and LEFT JOIN combined with an IS NULL condition.

Have you finished this lesson?

Introduction to SQL – All Lessons