Select rows from a table that are not in another
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with relational databases, a common requirement involves selecting rows from one table that do not exist within another table. This is known as performing a "set difference" or executing an "anti-join" operation. Various SQL techniques can achieve this, each with its own advantages and limitations. This article explores the concept in detail, providing technical explanations and examples to illustrate how you can implement this operation in different scenarios.
Conceptual Overview
The primary goal of selecting rows from one table that are not present in another is to identify mismatches or unique entries that need resolution, further analysis, or exclusion. For instance, consider two tables:
- `Orders`: Contains all orders placed in a system.
- `ShippedOrders`: Contains all orders that have been shipped.
The task may involve identifying orders that haven't been shipped yet. This is achievable through various SQL approaches such as subqueries, `LEFT JOIN` with `IS NULL`, and the `EXCEPT` clause.
Techniques to Select Rows Not in Another Table
Subqueries and `NOT IN`
The classic approach involves using a subquery with the `NOT IN` operator. This method checks each entry in the first table against a list of values from the second table. Here's an example query:
- Null Handling: If the subquery returns `NULL`, it impacts the `NOT IN` logic since SQL compares `NULL` values using three-valued logic. If the subquery involves nullable fields, it's safer to use alternatives.
- Performance: Using a join can be more efficient, especially if both tables are indexed on the column involved.
- This approach is more predictable with `NULL` values in the join column compared to using `NOT IN` without explicit `NULL` checks.
- Simplicity: This syntax is straightforward and mirrors set theory.
- Portability: Not all SQL databases support the `EXCEPT` operation.
- Using `LEFT JOIN`:
- Using Subqueries:
- Indexes on key columns can substantially enhance performance, especially for join operations.
- The database's execution plan should be examined to ensure the chosen approach is optimal.
- Database-specific optimizations and query hints may provide additional performance benefits.
Related reading
- SELECT WHERE NOT EXISTS
- Selecting a row of pandas series/dataframe by integer index
- Selecting columns from 3D tensor according to a 1D tensor of indices Tensorflow
- selecting rows with id from another table
- selecting unique values from a column
- Selection of database
- Send settings to clickhouse via http protocol using requests
- Sequel Pro and MySQL connection failed

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.