Sub topic 12
SQL Inner Joins on Ranges and Inequalities
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also understand primary keys, foreign keys, and ordinary equality joins.
An inner join does not require an equality condition. A non-equi join is a join whose ON condition uses operators such as:
>,<,>=, or<=BETWEEN- A combination of conditions joined with
AND
This is useful when one table stores ranges and another table stores values that must fall inside those ranges.
Matching orders to discount tiers
Suppose each order receives a discount tier based on its total. The tier table stores a lower boundary, an upper boundary, and a label.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
total DECIMAL(10, 2) NOT NULL
);
CREATE TABLE discount_tiers (
id INTEGER PRIMARY KEY,
min_total DECIMAL(10, 2) NOT NULL,
max_total DECIMAL(10, 2) NOT NULL,
label VARCHAR(30) NOT NULL
);
INSERT INTO orders (id, total) VALUES
(101, 20.00),
(102, 75.00),
(103, 150.00),
(104, 300.00);
INSERT INTO discount_tiers (id, min_total, max_total, label) VALUES
(1, 0.00, 50.00, 'No discount'),
(2, 50.00, 100.00, 'Silver'),
(3, 100.00, 250.00, 'Gold');
SELECT o.id, o.total, t.label
FROM orders AS o
INNER JOIN discount_tiers AS t
ON o.total >= t.min_total
AND o.total < t.max_total
ORDER BY o.id;The condition uses a half-open range: the minimum is included, but the maximum is excluded. The results are:
| order id | total | label |
|---|---|---|
| 101 | 20.00 | No discount |
| 102 | 75.00 | Silver |
| 103 | 150.00 | Gold |
Order 104 has a total of 300.00. It does not fit any tier, so the inner join excludes it.
Why boundary choices matter
The ranges above are adjacent:
0.00 <= total < 50.0050.00 <= total < 100.00100.00 <= total < 250.00
A total of exactly 50.00 matches only the Silver tier. If both ranges used inclusive boundaries, such as total >= min_total AND total <= max_total, a total of 50.00 could match both the first and second tiers. That would multiply the order into two result rows.
A range table should normally have non-overlapping rows. You can check for overlapping ranges separately before using it in a join.
flowchart TD
A[Order total] --> B{Matches min_total <= total?}
B -->|No| C[Try another tier]
B -->|Yes| D{total < max_total?}
D -->|No| C
D -->|Yes| E[Return the matching tier]Other inequality joins
You can compare columns from two tables in many ways. For example, an employee may be joined to a salary grade when the employee's salary falls within that grade's range:
SELECT e.name, e.salary, g.grade
FROM employees AS e
INNER JOIN salary_grades AS g
ON e.salary >= g.minimum_salary
AND e.salary < g.maximum_salary;The same pattern can match events to active subscriptions:
SELECT e.id, s.customer_id
FROM events AS e
INNER JOIN subscriptions AS s
ON e.occurred_at >= s.started_at
AND e.occurred_at < s.ended_at;If either compared value is NULL, the condition is not true, so the row does not match an inner join.
Exercise
A tier has mintotal = 100 and maxtotal = 200. Does an order totaling exactly 200 match the condition total >= mintotal AND total < maxtotal?
Show answer
No. The lower boundary is included, but the upper boundary is excluded.