

ON r1.preferred_district = r2.preferred_district AND r1.id != r2.id Īs you can see in this example, we joined the renters table with itself. SELECT r1.name, r1.preferred_district, r2.name, r2.preferred_district Thus, we want to list all possible pairs of our renters along with their preferred district to see which renters could potentially rent a house together. Let’s imagine that our renters are ready to consider sharing a house with another family. We’re going to start with the most common use cases for non-equi JOINs. You can use non-equi JOINs to list all (unique) pairs of items, identify duplicates, list items within a certain range of values or between certain dates, compute running totals, and more. If you have never used non-equi JOINs before, you may wonder what the common scenarios for applying this non-standard type of JOIN are. Now, we are ready to move to non-equi JOIN examples. deals with the deal ID, date, renter ID, house ID, and agent fee received from the corresponding deal.

renters with the renter ID, name, preferred district, minimum number of bedrooms required, and acceptable rent range.houses with the house ID, district, address, number of bedrooms, and rent.We have three tables in our imaginary database: We’re going to do some data analytics for a real estate agency that runs a rental business. Getting to Know the Dataīefore diving into different examples of non-equi JOINs, let’s first see the data that we’ll be exploring in this article. Comparison operators, like, =, !=, and and the BETWEEN operator work perfectly for joining tables in SQL.Ĭheck out this illustrated guide to the SQL non-equi JOIN for a better understanding of how it works. When you join two tables using other conditional operators, beyond the equal sign, non-equi JOINs come into play. Such joins are called non-equi JOINs, and they are also possible in SQL.
