Non-equi joins in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use non-equi joins in SQL, the database compares rows based on conditions other than simple equality.
We want to understand how the time to run these joins grows as the data gets bigger.
Analyze the time complexity of the following SQL query using a non-equi join.
SELECT a.id, b.value
FROM TableA a
JOIN TableB b
ON a.score > b.min_score
AND a.score < b.max_score;
This query joins two tables where the score in TableA falls between min_score and max_score in TableB.
Look for repeated checks or loops in the join process.
- Primary operation: For each row in TableA, the database checks multiple rows in TableB to find matches.
- How many times: This check happens for every row in TableA against many rows in TableB.
As the number of rows in both tables grows, the number of comparisons grows quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The work grows much faster than the number of rows, roughly multiplying the sizes of both tables.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the number of rows in the first table by the number in the second.
[X] Wrong: "Non-equi joins run as fast as simple equality joins."
[OK] Correct: Non-equi joins often require checking many rows against each other, which takes more time than matching exact values.
Understanding how join conditions affect performance helps you write better queries and explain your reasoning clearly in interviews.
"What if we added an index on TableB's min_score and max_score columns? How would the time complexity change?"
Practice
non-equi join in SQL?Solution
Step 1: Understand join conditions
Equi joins use equality (=) to match rows. Non-equi joins use other operators like <, >, or BETWEEN.Step 2: Identify non-equi join definition
Since non-equi joins match rows based on inequalities or ranges, A join that uses conditions other than equality, like <, >, or BETWEEN. correctly describes this.Final Answer:
A join that uses conditions other than equality, like <, >, or BETWEEN. -> Option AQuick Check:
Non-equi join = condition other than = [OK]
- Confusing non-equi join with equi join
- Thinking non-equi join matches all rows
- Assuming only AND operator defines non-equi join
Solution
Step 1: Recall BETWEEN syntax
BETWEEN is used as: column BETWEEN low AND high, without extra operators.Step 2: Check each option
SELECT * FROM A JOIN B ON A.value BETWEEN B.min AND B.max; uses correct syntax: A.value BETWEEN B.min AND B.max. Others misuse BETWEEN or add extra operators.Final Answer:
SELECT * FROM A JOIN B ON A.value BETWEEN B.min AND B.max; -> Option DQuick Check:
BETWEEN syntax = column BETWEEN low AND high [OK]
- Adding = before BETWEEN
- Using IN BETWEEN instead of BETWEEN
- Placing BETWEEN incorrectly in ON clause
Products(product_id, price) and Discounts(min_price, max_price, discount_rate), what does this query return?SELECT p.product_id, d.discount_rate FROM Products p JOIN Discounts d ON p.price >= d.min_price AND p.price < d.max_price;
Solution
Step 1: Analyze join condition
The join matches products where price is between min_price (inclusive) and max_price (exclusive).Step 2: Understand result
This returns products with their discount rate if their price falls in the discount's price range.Final Answer:
All products with their matching discount rate based on price ranges. -> Option CQuick Check:
Non-equi join matches price ranges = All products with their matching discount rate based on price ranges. [OK]
- Thinking only exact matches are returned
- Assuming all products join with all discounts
- Believing the query has syntax errors
SELECT e.name, s.salary_grade FROM Employees e JOIN SalaryGrades s ON e.salary => s.min_salary AND e.salary <= s.max_salary;
Solution
Step 1: Check operators in join condition
The operator => is not valid SQL; the correct operator for 'greater than or equal' is >=.Step 2: Verify other parts
AND is correct to check salary between min and max. Aliases and WHERE clause are not errors here.Final Answer:
The operator => is invalid; it should be >=. -> Option BQuick Check:
Use >=, not => for greater or equal [OK]
- Typing => instead of >=
- Replacing AND with OR incorrectly
- Confusing alias usage in SELECT
Scores(student_id, score) and a table Grades(grade, min_score, max_score). Write a query to assign each student their grade based on their score using a non-equi join.Which query correctly implements this?
Solution
Step 1: Understand grading ranges
Grades are assigned where score is between min_score (inclusive) and max_score (exclusive) to avoid overlap.Step 2: Check each join condition
SELECT s.student_id, g.grade FROM Scores s JOIN Grades g ON s.score >= g.min_score AND s.score < g.max_score; uses s.score >= g.min_score AND s.score < g.max_score, correctly defining non-overlapping ranges.Step 3: Verify other options
SELECT s.student_id, g.grade FROM Scores s JOIN Grades g ON s.score BETWEEN g.min_score AND g.max_score; includes max_score in BETWEEN (inclusive), which may cause overlap. SELECT s.student_id, g.grade FROM Scores s JOIN Grades g ON s.score > g.min_score AND s.score <= g.max_score; reverses inclusivity. SELECT s.student_id, g.grade FROM Scores s JOIN Grades g ON s.score <= g.min_score AND s.score >= g.max_score; reverses logic incorrectly.Final Answer:
SELECT s.student_id, g.grade FROM Scores s JOIN Grades g ON s.score >= g.min_score AND s.score < g.max_score; -> Option AQuick Check:
Use >= min and < max for non-overlapping ranges [OK]
- Using BETWEEN which includes max_score causing overlap
- Swapping < and > operators
- Using incorrect inclusivity causing duplicate grades
