Home / Computer Science / SQL Server / Anti Join in SQL: Definition, Usage and Example for Data Analysis

Anti Join in SQL: Definition, Usage and Example for Data Analysis

In SQL, an anti join is a type of JOIN that returns only the rows from one table that do not have matching rows in the other table. This means that an anti join is useful when you want to find records that are present in one table but not in another.

To illustrate an anti join in SQL, consider the following two tables: “customers” and “orders”. The “customers” table contains information about customers, including their name, address, and ID. The “orders” table contains information about orders, including the customer ID, order number, and order date.

Suppose you want to find all the customers who have not placed an order in the last month. You can do this using an anti join as follows:

SELECT *
FROM customers
LEFT JOIN orders
ON customers.ID = orders.customer_ID
WHERE orders.order_date < '2023-04-21' OR orders.order_date IS NULL;

In this example, we are performing a LEFT JOIN between the “customers” table and the “orders” table on the “customer_ID” column. The WHERE clause includes a condition that specifies that the order date must be less than ‘2023-03-23’ (i.e., the date one month ago) or null. This will return all the customers who either have not placed an order at all or have not placed an order in the last month.

Alternatively, you can use the NOT EXISTS operator to achieve the same result:

SELECT *
FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders
WHERE customers.ID = orders.customer_ID
AND orders.order_date >= '2023-04-21'
);

In this example, we are using the NOT EXISTS operator to return all the customers for whom there is no corresponding order in the “orders” table with an order date greater than or equal to ‘2023-04-21’.

Both of these queries return the same result: a list of customers who have not placed an order in the last month. This is an example of how you can use an anti join to find records that are present in one table but not in another, making it a valuable tool for data analysis in SQL.

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

Date and Time Functions in SQL Server

Introduction: In the world of databases, managing date and time is a crucial aspect. SQL …