What is semi-join in database?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the realm of relational database management systems, joins play a critical role in combining data from two or more tables based on a related column between them. Among the various types of joins, the semi-join offers a unique method of retrieving data. A semi-join, in essence, is a two-step process that aids in optimizing query performance by reducing the amount of data that needs to be handled during the join process. It is particularly useful when dealing only with large datasets where efficiency becomes pivotal.
Understanding Semi-Join
A semi-join between two tables returns rows from the first table where a condition matches rows in the second table. However, unlike standard joins, the semi-join does not return any columns from the second table. Essentially, it filters the records from the first table based on the existence of corresponding values in the second table, yet only the columns from the first table are displayed.
Technical Explanation
Consider two tables, Customers and Orders. The Customers table has columns CustomerID and CustomerName, while the Orders table contains OrderID, CustomerID, and OrderDate. If you want to find all customers who have placed at least one order, you can use a semi-join.
SQL Representation
Using SQL, a semi-join can be approximated using the IN or EXISTS clause. The SQL query below demonstrates a semi-join using the EXISTS keyword:
This query checks for the existence of each customer in the orders table and fetches the corresponding CustomerID and CustomerName from the Customers table where at least one matching CustomerID exists in the Orders table.
Benefits of Semi-Join
The main advantage of using a semi-join is the improvement in query performance, especially when the second table (in the above case, Orders) is significantly larger than the first. By using a semi-join, the database system needs to process fewer rows and does not need to perform a complete join, which involves combining rows from both tables.
Applications of Semi-Join
Semi-joins are particularly useful in distributed database environments where minimizing data transmission between servers can lead to significant improvements in performance. When only the existence of related rows, and not their content, needs to be verified across databases, semi-joins can efficiently handle such requirements.
Comparison with Other Joins
Unlike inner joins or outer joins that physically combine rows from both tables based on the join condition, a semi-join restricts the result to entries from only the primary table. Additionally, while an anti-join returns rows from the first table that have no corresponding entries in the second table, a semi-join returns those that do have matching entries.
Summary Table
| Feature | Semi-Join | Inner Join | Anti-Join |
| Result Columns | Only from first table | From both tables | Only from first table |
| Matching rows | Only if matching in second table | Only if matching in both tables | Only if no match in second table |
| Use Case | Data existence check | Data combination | Data exclusion check |
| Performance | High on large datasets | Variable, depending on dataset size | High on filtered datasets |
Conclusion
Semi-joins are a powerful tool in SQL and database design, offering a performance-optimized way of querying information where only the existence of a relation is necessary, without requiring the data from the related table. Understanding when and how to use semi-joins can greatly enhance the efficiency of database queries, especially in complex or distributed database systems.
Related reading
- What is system.size_estimates in cassandra and plausible reasons behind high disk consumption
- What is the algorithm for query search in the database?
- What is the benefit of zerofill in MySQL?
- What is the best api/library for Java to use Cassandra?
- What is the best collation to use for MySQL with PHP?
- What is the best way to check if table exists in DynamoDB?
- What is the best way to distribute postgresql
- What is the biggest Couchbase cluster nodes number?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.