Beginner

579. EXISTS with JOIN

Learn how EXISTS can be combined with JOIN logic to check whether related records satisfy additional conditions.

📘 Lesson 579 of 728 ⏱ 14 min 🎯 Advanced

EXISTS checks whether a subquery returns at least one row. The subquery can itself contain JOINs to evaluate complex relationships.

EXISTS is useful when the requirement is to confirm the existence of qualifying related data without returning duplicate parent rows.

Find customers who have purchased products from a specific category.

SQL
SELECT
    c.CustomerID,
    c.CustomerName
FROM Customers c
WHERE EXISTS (
    SELECT 1
    FROM Orders o
    INNER JOIN Products p
        ON o.ProductID = p.ProductID
    WHERE o.CustomerID = c.CustomerID
      AND p.Category = 'Electronics'
);

Used in customer segmentation, eligibility rules, compliance checks, and business validation.

  • EXISTS checks for at least one matching row.
  • The EXISTS subquery can contain JOINs.
  • It can avoid duplicate parent rows.
  • It is useful for conditional existence checks.
  • The subquery does not need to return business columns.
Why use EXISTS instead of a normal JOIN?

EXISTS can check whether a related record exists without returning multiple parent rows when several matches are present.

Can an EXISTS subquery contain a JOIN?

Yes. The EXISTS subquery can contain JOINs and additional filters to verify complex relationships.

Have you finished this lesson?

Introduction to SQL – All Lessons