Course contentsShow
← Databases & SQL
Lesson 203 of 1,9533. Relational Algebra in PracticeFree lesson

Theta Joins for Non-Equi Relationships

Join datasets using complex inequality conditions instead of simple equality.

In this lesson, you will learn how to combine datasets based on relationships other than simple equality, such as "greater than" or "less than."

Moving Beyond Equality

So far, we have relied heavily on the INNER JOIN using the equality operator (=). We call these "Equi-Joins." However, real-world data often requires us to relate tables based on ranges or thresholds. This is where the Theta Join comes in.

A Theta Join is defined by a condition (θ\theta) that can involve any comparison operator, such as <, >, <=, or >=. Instead of looking for a perfect match, you are looking for a logical relationship between attributes across two tables.

A Practical Example: Shipping Rates

Imagine you have a table called Orders (with an order_weight) and a table called ShippingRates (with a min_weight and max_weight). You want to match each order to the correct rate category. An equality join won't work here because the weight of the order will rarely be exactly the same as a threshold number.

Instead, we use a Theta Join:

SELECT *
FROM Orders O
JOIN ShippingRates S 
  ON O.order_weight >= S.min_weight 
  AND O.order_weight <= S.max_weight;

Why it Matters

In Relational Algebra, a Theta Join is expressed as a Selection performed on a Cartesian Product. Effectively, the database generates all possible combinations of rows from both tables and then filters them based on your specific inequality.

While Equi-Joins are standard, Theta Joins allow you to solve complex problems like "finding all employees hired within three months of another" or "matching orders to specific price tiers." By using > or < instead of =, you transform the join from a simple matching tool into a powerful analytical engine capable of comparing data across non-identical values.

Key Takeaway: Theta Joins enable you to associate records based on mathematical comparisons rather than equality, making them essential for range-based queries and interval analysis.