Challenge - 5 Problems
Non-equi Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a non-equi join with range condition
Given two tables Products and Discounts, what is the output of the following query?
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
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;
Attempts:
2 left
💡 Hint
Think about how BETWEEN works and matches the price ranges in Discounts.
✗ Incorrect
The join condition matches each product price to the discount range it falls into. Product 1 price 50 is between 0 and 100, so discount 0.05 applies. Product 2 price 150 is between 101 and 200, so discount 0.10 applies. Product 3 price 250 is between 201 and 300, so discount 0.15 applies.
📝 Syntax
intermediate1: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;
Attempts:
2 left
💡 Hint
Check if using AND and comparison operators in JOIN ON clause is allowed.
✗ Incorrect
SQL allows complex conditions with AND and comparison operators in JOIN ON clauses. The query is syntactically correct.
❓ optimization
advanced2: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:
Which optimization technique will improve query performance the most?
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;
Attempts:
2 left
💡 Hint
Think about how indexes help with range queries in joins.
✗ Incorrect
Indexes on the columns used in the join condition help the database quickly find matching rows for the range conditions, improving performance.
🔧 Debug
advanced2:00remaining
Debugging unexpected results in a non-equi join
A query joins Employees and SalaryGrades on salary ranges:
Some employees appear multiple times with different grades. What is the most likely cause?
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;
Attempts:
2 left
💡 Hint
Check if salary ranges in SalaryGrades overlap.
✗ Incorrect
If salary ranges overlap, an employee's salary can match multiple grades, causing multiple rows per employee.
🧠 Conceptual
expert3:00remaining
Understanding non-equi join behavior with NULL values
Consider the query:
If some rows in TableB have NULL in LowerBound or UpperBound, what will happen to those rows in the join result?
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;
Attempts:
2 left
💡 Hint
Remember how NULL behaves in comparison operations in SQL.
✗ Incorrect
Any comparison with NULL results in UNKNOWN, so the join condition fails and those rows do not match.