Web20 University

How to Use the MySQL EXCEPT Operator

Get up to 65% Off Hosting with FREE SSL & FREE Domains!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

When working with databases, there are often situations where you want to compare and retrieve data that exists in one dataset but not in another. In SQL, this can be done using the EXCEPT operator, which allows you to return the rows from one query that are not present in another. However, if you’re using MySQL, you may have noticed that MyS

In databases like PostgreSQL, SQLite, and SQL Server, the EXCEPT operator is used to compare two datasets and return only the rows that exist in the first dataset but not in the second. It works similarly to the UNION operator but performs the opposite action—UNION combines rows from two queries, while EXCEPT subtracts one set from another.

Basic EXCEPT Syntax (in other SQL databases):

SELECT column_list FROM table1
EXCEPT
SELECT column_list FROM table2;

This query would return all the rows from table1 that are not in table2. Unfortunately, MySQL does not directly support the EXCEPT operator, but we can achieve the same effect using alternative approaches.

How to Use the EXCEPT Operator in MySQL (Using Workarounds)

Since MySQL doesn’t have a built-in EXCEPT operator, you can mimic its functionality using either LEFT JOIN with IS NULL or the NOT IN clause. Both methods compare the results of two queries, providing the same outcome as EXCEPT.

Method 1: Using LEFT JOIN and IS NULL

A common workaround to simulate the EXCEPT operator is using a LEFT JOIN along with the IS NULL condition. The idea here is to join the two tables and filter out any rows that exist in both tables.

Example Query:

SELECT t1.column_list
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL;

Explanation:

  • We perform a LEFT JOIN between table1 and table2 based on a common column (id in this case).
  • The WHERE t2.id IS NULL condition filters out rows that exist in both table1 and table2, leaving us only with the rows from table1 that are not present in table2.

Method 2: Using NOT IN

Another way to simulate the EXCEPT operator in MySQL is by using the NOT IN clause. This is a simpler approach, where you exclude rows from one query that exist in another.

Example Query:

SELECT column_list
FROM table1
WHERE id NOT IN (SELECT id FROM table2);

Explanation:

  • The NOT IN clause checks whether the id values from table1 are not present in the subquery that selects id values from table2.
  • This will return the rows from table1 that do not have matching ids in table2.

Both approaches achieve the same goal of filtering out rows present in the second dataset.


Examples of Using EXCEPT in MySQL

Let’s look at a couple of examples to illustrate how these methods work in real-world scenarios.

Example 1: Find Users in One Table but Not Another

Suppose you have two tables: users_2023 and users_2024. You want to find users who were present in 2023 but not in 2024.

Using LEFT JOIN and IS NULL:

SELECT u23.user_id, u23.name
FROM users_2023 u23
LEFT JOIN users_2024 u24 ON u23.user_id = u24.user_id
WHERE u24.user_id IS NULL;

This query returns users from the users_2023 table who are not present in the users_2024 table.

Using NOT IN:

SELECT user_id, name
FROM users_2023
WHERE user_id NOT IN (SELECT user_id FROM users_2024);

Both queries return the same result: users who are in the 2023 table but not in the 2024 table.

Example 2: Find Orders Present in One Year but Not Another

Imagine you have two tables, orders_2022 and orders_2023, and you want to find the orders that were placed in 2022 but not in 2023.

Using LEFT JOIN and IS NULL:

SELECT o22.order_id, o22.product
FROM orders_2022 o22
LEFT JOIN orders_2023 o23 ON o22.order_id = o23.order_id
WHERE o23.order_id IS NULL;

Using NOT IN:

SELECT order_id, product
FROM orders_2022
WHERE order_id NOT IN (SELECT order_id FROM orders_2023);

Both queries will return the orders from 2022 that are not present in the 2023 dataset.

Conclusion

While MySQL does not have a native EXCEPT operator, you can effectively replicate its behavior using other techniques like LEFT JOIN with IS NULL or the NOT IN clause. Both approaches provide the same result by comparing datasets and returning only those rows that exist in one table and not the other.

Whether you’re working with user lists, order histories, or any other type of data comparison, these methods allow you to achieve the functionality of EXCEPT in MySQL, helping you handle complex data queries with ease.

Further Reading:

Get up to 65% Off Hosting with FREE SSL & FREE Domains!