How to Use SQL EXISTS and NOT EXISTS Correlated Subqueries

Relational database showing customer links to orders and support requests with matching and absent records highlighted.

What You’ll Learn

In this lesson, you’ll learn how to use SQL EXISTS and NOT EXISTS correlated subqueries to filter rows according to whether related records are present.

  • Use EXISTS to find customers who have placed at least one order.
  • Use NOT EXISTS to find customers who have never submitted a support request.
  • Understand how correlated subqueries connect an outer query to a related table.
  • Avoid common mistakes involving missing correlation and NULL values.
Ad

The Concept

An EXISTS subquery tests whether a subquery returns at least one row. It does not return the related rows themselves; it returns a true or false result for each row considered by the outer query.

For example, when checking customers who have orders, the database evaluates the subquery for each customer:

  • If at least one matching order exists, the customer passes the filter.
  • If no matching order exists, the customer is excluded.

NOT EXISTS reverses this test. It keeps an outer row only when the subquery returns no matching rows.

These are usually written as correlated subqueries. A correlated subquery refers to a column from the outer query, such as the current customer’s identifier:

The pattern looks like this:

SELECT columns
FROM parent_table AS p
WHERE EXISTS (
    SELECT 1
    FROM related_table AS r
    WHERE r.parent_id = p.parent_id
);

The condition connecting r.parent_id to p.parent_id is what makes the subquery correlated. Without that condition, the database would test whether any related row exists anywhere in the table, rather than whether one exists for the current parent row.

Basic Example

Assume these tables are available:

  • customers contains customer_id and customer_name.
  • orders contains order_id and customer_id.
  • A customer’s identifier in orders refers to the customer who placed that order.

The following query returns customers who have placed at least one order:

SELECT
    c.customer_id,
    c.customer_name
FROM customers AS c
WHERE EXISTS (
    SELECT 1
    FROM orders AS o
    WHERE o.customer_id = c.customer_id
)
ORDER BY c.customer_id;

Expected Output

If customers 101, 103, and 104 have orders, the result is:

customer_id  customer_name
-----------  -------------
101          Maya Chen
103          Luis Romero
104          Aisha Patel

How the Code Works

The diagram starts with each customer row, then tests for a matching order and a matching support request using correlated relationship checks. Customers pass the EXISTS order test only when an order exists, and pass the NOT EXISTS support-request test only when no request exists. The final result contains customers who satisfy both conditions, without duplicate customer rows.
For each customer, correlated EXISTS and NOT EXISTS checks test related tables and return each qualifying customer once.

The outer query starts with every row in customers. The alias c is a short name for the current customer row.

For each customer, SQL evaluates this subquery:

SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id

The condition compares the order’s customer identifier with the current customer’s identifier. If one or more matching orders are found, EXISTS is true and the customer is included.

The value 1 in SELECT 1 is conventional. The query only needs to know whether a row exists, so it does not need to retrieve order columns. The database can stop looking after finding the first match.

Notice that a customer appears only once in the result, even if that customer has many orders. This is one advantage of using EXISTS when you need to test for a relationship rather than return related data.

Another Example

Now find customers who have never submitted a support request. This uses NOT EXISTS against a different related table:

SELECT
    c.customer_id,
    c.customer_name
FROM customers AS c
WHERE NOT EXISTS (
    SELECT 1
    FROM support_requests AS sr
    WHERE sr.customer_id = c.customer_id
)
ORDER BY c.customer_name;

For each customer, the subquery searches for a matching support request. A customer is returned only when that search finds no rows. This can be useful for identifying customers who may need onboarding help, have not contacted support, or have not reported an issue.

Common Mistakes

Forgetting the correlation condition

The subquery must connect the related table to the current outer row. If you omit the comparison involving c.customer_id, the query checks whether any order exists for any customer. That can cause every customer to pass or fail together.

Using NOT IN when NULL values are possible

A query using NOT IN can produce surprising results when the subquery returns a NULL. SQL’s three-valued logic treats comparisons with NULL as unknown. NOT EXISTS avoids this particular problem because it directly tests whether a matching row exists.

Using a join when only existence matters

A join can also find customers with orders, but one customer may appear once for every matching order. You may then need DISTINCT to remove duplicates. EXISTS expresses the intent more directly when the related columns are not needed in the result.

Ignoring indexes on relationship columns

For large tables, indexes on columns used for correlation, such as orders.customer_id and support_requests.customer_id, can make existence checks much faster. The exact execution plan depends on the database system, table sizes, and available indexes, so use your database’s query plan tools when performance matters.

Try It Yourself

Write a query that returns customers who have submitted at least one support request. Include the customer’s identifier and name, and sort the result by customer name.

Use EXISTS rather than joining the tables. The query should return each customer only once, even if that customer has submitted several requests.

Challenge

Write a query that finds customers who meet both conditions:

  • They have placed at least one order.
  • They have never submitted a support request.

Return customer_id and customer_name, sorted by customer_id. Use one EXISTS subquery and one NOT EXISTS subquery.

Solution

SELECT
    c.customer_id,
    c.customer_name
FROM customers AS c
WHERE EXISTS (
    SELECT 1
    FROM orders AS o
    WHERE o.customer_id = c.customer_id
)
AND NOT EXISTS (
    SELECT 1
    FROM support_requests AS sr
    WHERE sr.customer_id = c.customer_id
)
ORDER BY c.customer_id;

The first correlated subquery requires at least one matching order. The second requires that no matching support request exists. Because both tests are applied to the same outer customer row, the result contains only customers who satisfy both conditions.

Key Takeaways

  • EXISTS keeps an outer row when its correlated subquery finds at least one related row.
  • NOT EXISTS keeps an outer row when its correlated subquery finds no related rows.
  • The correlation condition connects the related table to the current row from the outer query.
  • Use existence tests when you need to filter by a relationship but do not need to return related-table columns.
  • Indexes on relationship columns can improve performance for large tables.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad