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.
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.