Non-equi joins: ranges and inequalities
Overview
A non-equi join uses a condition other than equality to match rows. Range joins classify values into intervals, such as a product price band. In a half-open interval the lower boundary belongs to the interval and the upper boundary does not.
First example
SELECT p.id, b.labelFROM products AS p JOIN join_price_bands AS b ON p.price_cents >= b.lo AND p.price_cents < b.hiORDER BY p.id;Observed SQLite result (rows in JSON):
[ [ 1, "standard" ], [ 2, "budget" ], [ 3, "premium" ], [ 4, "budget" ]]How it works
The bands are [0,200), [200,1000), and [1000,3000) cents. The first example classifies all four products. The second uses a value exactly at the boundary: 200 belongs only to standard. Overlapping ranges may produce several matches; gaps may produce none. An INNER JOIN omits unmatched prices; LEFT JOIN preserves them. For temporal joins use the same half-open logic with comparable timestamps (start <= event AND event < end); handle open-ended periods deliberately. A CHECK on each interval does not prevent overlap between rows. Do not use inclusive BETWEEN on adjacent ranges when each value must match exactly one band.
Practical example
SELECT b.label FROM join_price_bands AS bJOIN (SELECT 200 AS price) AS x ON x.price >= b.lo AND x.price < b.hiORDER BY b.id;Observed SQLite result (rows in JSON):
[ [ "standard" ]]Practice and mistakes
Practice: predict the band for 199, 200 and 3000 cents; 3000 has no match. Use LEFT JOIN to expose unmatched values. Check that the number of matches per product is exactly one before trusting the classification.