Understanding NOT EXISTS in MySQL: A Beginner's Guide

Understanding NOT EXISTS in MySQL: A Beginner's Guide
Photo by Fabrizio Frigeni / Unsplash

In MySQL, the NOT EXISTS condition is a useful tool for filtering data based on the absence of a certain relationship between two tables. It allows you to query rows from one table that do not have corresponding entries in another table. This is particularly helpful when you need to find data that is missing from one table but present in another.

What is NOT EXISTS?

NOT EXISTS is used in a subquery to check for non-existence. It returns TRUE if the subquery does not return any rows. If the subquery finds a row, NOT EXISTS returns FALSE.

Basic Syntax of NOT EXISTS

SELECT columns
FROM table1
WHERE NOT EXISTS (subquery);

subquery: This is a query that checks whether certain rows exist in a related table. If no matching rows are found, NOT EXISTS returns TRUE for the outer query.

Simple Example

Let’s say you are managing a small online store. You have two tables: customers and orders.

  1. customers table (list of all customers):
    • customer_id: The unique ID of each customer.
    • name: The name of the customer.
  2. orders table (list of orders made by customers):
    • order_id: The unique ID of each order.
    • customer_id: The ID of the customer who made the order.

You want to find out which customers have never made any orders. Here’s how you can use NOT EXISTS to accomplish this.

Example Tables

customers Table:

customer_idname
1Alice
2Bob
3Charlie
4Diana

orders Table:

order_idcustomer_id
1011
1022

In this example:

  • Alice and Bob have made orders (their customer_id exists in the orders table).
  • Charlie and Diana have not made any orders.

Query Using NOT EXISTS

We can use NOT EXISTS to find customers who do not have any matching rows in the orders table.

SELECT c.name
FROM customers c
WHERE NOT EXISTS (
    SELECT 1
    FROM orders o
    WHERE o.customer_id = c.customer_id
);

Explanation of the Query:

  1. Outer query:
    • We are selecting name from the customers table (aliased as c).
    • We want to return customers whose customer_id does not have a matching entry in the orders table.
  2. Subquery:
    • The subquery checks the orders table to see if a matching customer_id exists.
    • If it finds a matching row, the NOT EXISTS condition will return FALSE, meaning that customer has placed an order.
  3. Result:
    • If the subquery finds no matching rows (i.e., the customer has not placed any orders), the outer query will return that customer’s name.

Output of the Query:

name
Charlie
Diana

The result shows that Charlie and Diana have not made any orders.

Why Use NOT EXISTS?

NOT EXISTS is preferred when you want to ensure that the subquery checks for non-existence, as opposed to using alternatives like LEFT JOIN with IS NULL. The performance of NOT EXISTS can be better in some scenarios, especially when indexes are properly applied on the related columns.

Finally

The NOT EXISTS clause is a powerful way to query for records that do not exist in another table. It’s helpful when you need to filter out data that lacks certain relationships.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe