Open In App

MySQL EXCEPT Operator

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Comparison of two different sets of data in a database and finding entries unique to one set is one of the common operations done on databases. The EXCEPT operator in SQL compares the two result sets returned and returns only the rows that are found in one but not the other.

Moreover, MySQL, one of the most popular relational database management systems, does not support the EXCEPT operator directly. Not to say that a MySQL user can't do anything differently; it provides alternative ways to get the same results from MySQL, such as using LEFT JOIN or NOT IN.

What is the EXCEPT Operator

The EXCEPT operator in SQL is used for comparing two result sets and returning the rows available in the first result set and not in the second. It does what one might think of as DIFF between two datasets. For this reason, it could be relevant for purposes of data validation, cleaning, and synchronization. This construct is in wide use in SQL for precisely this purpose, but MySQL does not have native support for it. Instead, other methods can be applied to achieve similar results and thus help MYSQL users, for example, LEFT JOIN or NOT IN. Being aware of these alternatives allows a MYSQL user to compare complex data and realize additional potential for database management.

Syntax of MySQL EXCEPT Operator

SELECT column1, column2, ...
FROM table1
EXCEPT
SELECT column1, column2, ...
FROM table2;

where,

  • SELECT column1, column2: This part of the query selects specific columns
  • EXCEPT: This can be achieved using the EXCEPT operator to find the difference between two SELECT statements.

MySQL Equivalent of the EXCEPT Operator

Since MySQL doesn't support an EXCEPT operator directly connected, we may do the same by use of other kinds of SQL constructs. The most common method applies by making use of LEFT JOIN or NOT IN clauses.

Using LEFT JOIN

As the name suggests, the LEFT JOIN approach joins two tables before filtering out the rows which turn up alike in the second table.

SELECT a.*
FROM table1 a
LEFT JOIN table2 b ON a.id = b.id
WHERE b.id IS NULL;

Using NOT IN

The NOT IN clause can also be used to filter out the matching rows.

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

Example of MySQL EXCEPT Operator

Here is an example of MySQL EXCEPT Operator:

Creating Tables

-- Create employees table
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100)
);

-- Create managers table
CREATE TABLE managers (
id INT PRIMARY KEY,
name VARCHAR(100)
);

Insert Data into Tables

-- Insert data into employees table
INSERT INTO employees (id, name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');

-- Insert data into managers table
INSERT INTO managers (id, name) VALUES
(1, 'Alice'),
(3, 'Charlie');

Output

Employees:

ID

Name

1

Alice

2

Bob

3

Charlie

Managers:

ID

Name

1

Alice

2

Charlie

Now we will query the data using the LEFT JOIN and NOT IN:

Using LEFT JOIN

-- Using LEFT JOIN to find employees who are not managers
SELECT e.id, e.name
FROM employees e
LEFT JOIN managers m ON e.id = m.id
WHERE m.id IS NULL;

Using NOT IN

SELECT id, name
FROM employees
WHERE id NOT IN (SELECT id FROM managers);

Using NOT EXISTS

SELECT id, name
FROM employees e
WHERE NOT EXISTS (SELECT 1 FROM managers m WHERE e.id = m.id);

Output:

ID

Name

2

Bob

Explanation:

The example here creates two tables: employees and managers. The employees table simply contains all employees, while the managers table only includes those who are managers. After filling these two tables with sample data, we create three alternative queries to find those employees who are not managers. The LEFT JOIN method joins the two tables, then selects staff without a matching entry in the managers table. The NOT IN method just selects staff whose IDs do not show in the list of manager IDs. NOT EXISTS ensures an employee ID does not exist in the managers table. All three queries answer the same thing, which verifies that only Bob is an employee who is not a manager.

Another Example of MySQL EXCEPT Operator

Suppose we have two tables, orders and shipped_orders. We want to find all orders that have not been shipped yet.

Create Table Using CREATE statement

-- Create orders table
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_name VARCHAR(100),
order_date DATE
);

-- Create shipped_orders table
CREATE TABLE shipped_orders (
order_id INT PRIMARY KEY,
ship_date DATE
);

Insert Data Into Table

-- Insert data into orders table
INSERT INTO orders (order_id, customer_name, order_date) VALUES
(101, 'Alice', '2024-07-01'),
(102, 'Bob', '2024-07-02'),
(103, 'Charlie', '2024-07-03'),
(104, 'David', '2024-07-04');

-- Insert data into shipped_orders table
INSERT INTO shipped_orders (order_id, ship_date) VALUES
(101, '2024-07-05'),
(103, '2024-07-06');

Using LEFT JOIN

SELECT o.order_id, o.customer_name, o.order_date
FROM orders o
LEFT JOIN shipped_orders s ON o.order_id = s.order_id
WHERE s.order_id IS NULL;

Using NOT IN

SELECT order_id, customer_name, order_date
FROM orders
WHERE order_id NOT IN (SELECT order_id FROM shipped_orders);

Using NOT EXISTS

SELECT order_id, customer_name, order_date
FROM orders o
WHERE NOT EXISTS (SELECT 1 FROM shipped_orders s WHERE o.order_id = s.order_id);

Output:

order_id

customer_name

order_date

102

Bob

2024-07-02

104

David

2024-07-04

Explanation:

Here, a case is assumed for two tables: 'orders' and 'shipped_orders'. Orders are stored for all orders placed, whereas 'shipped_orders' keeps those orders that are shipped out. Now, it will be identified which of the orders have not yet been shipped using three types of SQL techniques: LEFT JOIN, NOT IN, and NOT EXISTS. All of these methods are going to return orders that are placed in the 'orders' table and not in 'shipped_orders'. Result: This will show that orders placed by Bob and David are not shipped.

Conclusion

While EXCEPT cannot be used directly in MySQL, you could implement this with alternative SQL techniques. LEFT JOIN, NOT IN, NOT EXISTS—these all find records that exist in one result set and not the other. Learning these alternatives will enable you to contrast tough cases in data and enhance your capability with respect to administration and analysis in MySQL databases. Understanding how to use these methods in MySQL is crucial for effective data manipulation and problem solving, and it will enable you to address most querying needs despite the lack of EXCEPT clause support.


Next Article
Article Tags :

Similar Reads