![]() |
In the context of SQL, Anti-join, and semi-join are two essential operations in relational databases used for querying and manipulating data. These operations focus on comparing data from two related tables, but they serve distinct purposes. In this article let us discuss these two operations in detail along with some examples. What is SQL Join?Join is an operation performed on the relations or tables that combine rows from two or more relations or tables based on the related columns between them. Different kinds of joins can be applied to the relations which include: Semi-joinSemi-join is a type of join that is applied to relations to join them based on the related columns. When semi-join is applied, it returns the rows from one table for which there are matching records in another related table. Characteristics:
Syntax: using `EXISTS`:
using `IN`:
Example: Let us consider two relations namely “Customers” and “Orders“. The Customers table contains the list of customer’s details along with their ID and name. The Orders table contains the details of the orders placed by the customers along with the customer ID, Order Id, and the name of the order placed by the customer. QueryCreating Customers Table: CREATE TABLE Customers ( Inserting into Customers Table: INSERT INTO Customers (Customer_ID, Customer_Name) VALUES ('01', 'Alice'); Customers table:
QueryCreating Orders Table: CREATE TABLE Orders ( Inserting into Orders Table:INSERT INTO Orders (Customer_ID, Order_ID, Order_Name) VALUES ('02', '101', 'Stationery'); Orders Table
Query for Semi-Join:SELECT Customers.Customer_ID, Customers.Customer_Name Output:
Anti-JoinAnti-join or Anti-semi-join is a type of join that is applied to relations to join them based on the related columns. When anti-join is applied, it returns the rows from one table for which there are no matching records in another related table. Characteristics:
Syntax:
Example: Let us consider the above example only which has Customers and Orders tables. Query for Anti-Join SELECT Customers.* Output:
Difference Between Anti-Join and Semi-Join
ConclusionAnti-join filters out rows based on the absence of matches in another table, while semi-join filters rows based on the existence of related records but only returns columns from the first table. Understanding the differences between these two operations is crucial for effective data analysis and reporting in relational databases. |
Reffered: https://www.geeksforgeeks.org
DBMS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |