0
0
SQLquery~20 mins

Non-equi joins in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Non-equi Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a non-equi join with range condition
Given two tables Products and Discounts, what is the output of the following query?

SELECT p.ProductID, p.Price, d.DiscountRate
FROM Products p
JOIN Discounts d ON p.Price BETWEEN d.MinPrice AND d.MaxPrice
ORDER BY p.ProductID;

Tables:
Products
ProductID | Price
1 | 50
2 | 150
3 | 250

Discounts
DiscountID | MinPrice | MaxPrice | DiscountRate
1 | 0 | 100 | 0.05
2 | 101 | 200 | 0.10
3 | 201 | 300 | 0.15
SQL
SELECT p.ProductID, p.Price, d.DiscountRate
FROM Products p
JOIN Discounts d ON p.Price BETWEEN d.MinPrice AND d.MaxPrice
ORDER BY p.ProductID;
A[{"ProductID":1,"Price":50,"DiscountRate":0.05},{"ProductID":2,"Price":150,"DiscountRate":0.15},{"ProductID":3,"Price":250,"DiscountRate":0.10}]
B[{"ProductID":1,"Price":50,"DiscountRate":0.05},{"ProductID":2,"Price":150,"DiscountRate":0.10},{"ProductID":3,"Price":250,"DiscountRate":0.15}]
C[{"ProductID":1,"Price":50,"DiscountRate":0.15},{"ProductID":2,"Price":150,"DiscountRate":0.05},{"ProductID":3,"Price":250,"DiscountRate":0.10}]
D[{"ProductID":1,"Price":50,"DiscountRate":0.10},{"ProductID":2,"Price":150,"DiscountRate":0.15},{"ProductID":3,"Price":250,"DiscountRate":0.05}]
Attempts:
2 left
💡 Hint
Think about how BETWEEN works and matches the price ranges in Discounts.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in a non-equi join query
Which option contains a syntax error in this non-equi join query?

SELECT e.EmployeeID, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.Salary > d.MinSalary AND e.Salary < d.MaxSalary;
SQL
SELECT e.EmployeeID, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.Salary > d.MinSalary AND e.Salary < d.MaxSalary;
ANo syntax error, query is valid
BMissing ON keyword before join condition
CUsing AND in join condition causes syntax error
DUsing > and < operators in join condition is invalid
Attempts:
2 left
💡 Hint
Check if using AND and comparison operators in JOIN ON clause is allowed.
optimization
advanced
2:30remaining
Optimizing a non-equi join with large tables
You have two large tables, Orders and ShippingZones. You want to join orders to shipping zones based on order weight falling within zone weight limits:

SELECT o.OrderID, sz.ZoneName
FROM Orders o
JOIN ShippingZones sz ON o.Weight >= sz.MinWeight AND o.Weight < sz.MaxWeight;


Which optimization technique will improve query performance the most?
SQL
SELECT o.OrderID, sz.ZoneName
FROM Orders o
JOIN ShippingZones sz ON o.Weight >= sz.MinWeight AND o.Weight < sz.MaxWeight;
ACreate indexes on Orders.Weight and ShippingZones.MinWeight, ShippingZones.MaxWeight
BRewrite join as a CROSS JOIN with WHERE filtering
CAdd a computed column in Orders for weight range and join on equality
DUse UNION ALL to split ShippingZones into multiple queries
Attempts:
2 left
💡 Hint
Think about how indexes help with range queries in joins.
🔧 Debug
advanced
2:00remaining
Debugging unexpected results in a non-equi join
A query joins Employees and SalaryGrades on salary ranges:

SELECT e.EmployeeID, e.Salary, sg.Grade
FROM Employees e
JOIN SalaryGrades sg ON e.Salary >= sg.MinSalary AND e.Salary <= sg.MaxSalary;


Some employees appear multiple times with different grades. What is the most likely cause?
SQL
SELECT e.EmployeeID, e.Salary, sg.Grade
FROM Employees e
JOIN SalaryGrades sg ON e.Salary >= sg.MinSalary AND e.Salary <= sg.MaxSalary;
AMissing GROUP BY clause to aggregate results
BEmployees table has duplicate EmployeeID values
CJOIN condition uses >= and <= instead of > and <
DSalary ranges in SalaryGrades overlap causing multiple matches
Attempts:
2 left
💡 Hint
Check if salary ranges in SalaryGrades overlap.
🧠 Conceptual
expert
3:00remaining
Understanding non-equi join behavior with NULL values
Consider the query:

SELECT a.ID, b.Category
FROM TableA a
JOIN TableB b ON a.Value > b.LowerBound AND a.Value < b.UpperBound;


If some rows in TableB have NULL in LowerBound or UpperBound, what will happen to those rows in the join result?
SQL
SELECT a.ID, b.Category
FROM TableA a
JOIN TableB b ON a.Value > b.LowerBound AND a.Value < b.UpperBound;
ARows with NULL bounds will cause the query to fail with an error
BRows with NULL bounds will match all rows in TableA
CRows with NULL bounds in TableB will never match any row in TableA
DRows with NULL bounds will match only rows in TableA where a.Value is NULL
Attempts:
2 left
💡 Hint
Remember how NULL behaves in comparison operations in SQL.