EXISTS is a SQL operator that returns TRUE if the associated subquery returns one or more rows. It is commonly used with correlated subqueries to test whether matching records exist.
Business reports often need to identify customers with orders, employees assigned to projects, or products with sales. EXISTS performs these checks efficiently.
Find customers who have placed at least one order in the Orders table.
SELECT
CustomerName
FROM Customers C
WHERE EXISTS
(
SELECT *
FROM Orders O
WHERE O.CustomerID = C.CustomerID
);
EXISTS is commonly used in customer analytics, inventory management, financial systems, healthcare applications, fraud detection, and reporting solutions.
- Checks whether matching records exist.
- Returns TRUE or FALSE.
- Uses a Subquery.
- Often faster than IN for large datasets.
- Commonly used with correlated queries.
- Stops searching after the first match.
- Useful for existence checking.
What does EXISTS return?
EXISTS returns TRUE if the subquery returns one or more rows.
When is EXISTS commonly used?
It is used to check whether matching records exist in another table.