1. What is a Non-Equi Join in Oracle?
A Non-Equi Join is a join in which tables are joined using operators other than the equality (=) operator.
Common operators used are:
- <
- >
- <=
- >=
- BETWEEN
- LIKE
- <>
A Non-Equi Join is commonly used for range-based matching rather than exact value matching.
Syntax
SELECT column_list
FROM table1 t1
JOIN table2 t2
ON t1.column_name BETWEEN t2.min_value
AND t2.max_value;
2. Why do we use a Non-Equi Join?
A Non-Equi Join is used to:
- Match values within ranges
- Determine salary grades
- Assign tax slabs
- Apply commission percentages
- Categorize data based on intervals
- Perform comparisons using relational operators
3. What is the syntax of a Non-Equi Join?
Example using BETWEEN:
SELECT e.employee_name,
g.grade
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary;
4. How does a Non-Equi Join work?
Suppose we have:
EMPLOYEES
| EMPLOYEE_ID | EMPLOYEE_NAME | SALARY |
|---|---|---|
| 101 | John | 3000 |
| 102 | David | 6000 |
| 103 | Scott | 9000 |
SALARY_GRADES
| GRADE | MIN_SALARY | MAX_SALARY |
|---|---|---|
| A | 1000 | 4000 |
| B | 4001 | 7000 |
| C | 7001 | 10000 |
Query:
SELECT e.employee_name,
e.salary,
g.grade
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary;
Output:
| EMPLOYEE | SALARY | GRADE |
|---|---|---|
| John | 3000 | A |
| David | 6000 | B |
| Scott | 9000 | C |
5. Why is it called a Non-Equi Join?
Because the join condition does not use the = operator.
Instead, it uses operators such as <, >, <=, >=, BETWEEN, LIKE, and <>.
6. What operators can be used in a Non-Equi Join?
| Operator | Example |
|---|---|
| < | e.salary < g.max_salary |
| > | e.salary > g.min_salary |
| <= | e.salary <= g.max_salary |
| >= | e.salary >= g.min_salary |
| BETWEEN | e.salary BETWEEN g.min_salary AND g.max_salary |
| LIKE | t1.code LIKE t2.pattern |
| <> | t1.value <> t2.value |
7. Difference between an Equi Join and a Non-Equi Join
| Equi Join | Non-Equi Join |
|---|---|
| Uses = | Uses <, >, BETWEEN, <=, etc. |
| Matches equal values | Matches ranges or inequalities |
| Most common join type | Used for range-based matching |
Equi Join:
SELECT *
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
Non-Equi Join:
SELECT *
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary;
8. Can a Non-Equi Join use aliases?
Yes. Aliases improve readability.
SELECT e.employee_name,
g.grade
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary;
9. Can a Non-Equi Join use WHERE?
Yes.
SELECT e.employee_name,
g.grade
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary
WHERE g.grade = 'B';
10. Can a Non-Equi Join use GROUP BY?
Yes.
SELECT g.grade,
COUNT(*) AS employee_count
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary
GROUP BY g.grade;
11. Can a Non-Equi Join use ORDER BY?
Yes.
SELECT e.employee_name,
g.grade
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary
ORDER BY g.grade;
12. Can a Non-Equi Join use multiple conditions?
Yes.
SELECT *
FROM table1 t1
JOIN table2 t2
ON t1.salary BETWEEN t2.min_salary
AND t2.max_salary
AND t1.location = t2.location;
13. Can a Non-Equi Join return duplicate rows?
Yes. If a row matches multiple rows in the joined table, multiple rows may be returned. To avoid this, ensure that ranges do not overlap when only one match is expected.
14. What are common uses of a Non-Equi Join?
- Salary grades
- Tax brackets
- Commission slabs
- Insurance premium ranges
- Age categories
- Discount ranges
- Credit score classifications
15. Real-Time Example – Income Tax Slabs
EMPLOYEE
| NAME | SALARY |
|---|---|
| John | 3500 |
| David | 6500 |
| Scott | 9000 |
TAX_SLAB
| SLAB | MIN | MAX |
|---|---|---|
| 5% | 1000 | 4000 |
| 10% | 4001 | 7000 |
| 20% | 7001 | 10000 |
Query:
SELECT e.employee_name,
e.salary,
t.slab
FROM employees e
JOIN tax_slab t
ON e.salary BETWEEN t.min
AND t.max;
16. Can a Non-Equi Join use multiple tables?
Yes.
SELECT e.employee_name,
g.grade,
d.department_name
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary
JOIN departments d
ON e.department_id = d.department_id;
17. What are the advantages of a Non-Equi Join?
- Supports range-based matching
- Useful for business rules involving intervals
- Flexible comparison conditions
- Widely used in reporting and analytics
- Can model tax, grading, and pricing systems
18. What are the disadvantages of a Non-Equi Join?
- Can be slower than an Equi Join on large datasets
- May require careful index design for good performance
- Overlapping ranges can produce duplicate or ambiguous matches
- More complex to understand and maintain than simple equality joins
19. Common Errors with Non-Equi Joins
| Error | Cause |
|---|---|
| ORA-00904 | Invalid column name |
| ORA-00942 | Table or view does not exist |
| ORA-00918 | Column ambiguously defined |
| ORA-00933 | SQL command not properly ended |
| ORA-01722 | Invalid number |
20. Non-Equi Join vs Other Joins
| Join Type | Description |
|---|---|
| Equi Join | Joins using = |
| Non-Equi Join | Joins using range or inequality operators |
| Inner Join | Returns only matching rows |
| Left Join | Returns all rows from the left table |
| Right Join | Returns all rows from the right table |
| Full Outer Join | Returns matching and non-matching rows |
| Cross Join | Returns every possible row combination |
| Self Join | Joins a table to itself |
21. Can a Non-Equi Join use the USING clause?
No. The USING clause only supports equality joins on columns with the same name.
Incorrect:
SELECT *
FROM employees
JOIN salary_grades
USING (salary);
Correct:
SELECT *
FROM employees e
JOIN salary_grades g
ON e.salary BETWEEN g.min_salary
AND g.max_salary;
22. Can a Non-Equi Join use NATURAL JOIN?
No. A NATURAL JOIN is based on equality between columns with the same names. For range-based joins, use the ON clause.
23. Interview Scenario
Question: Employees receive a bonus based on salary ranges stored in a BONUS_RULES table. Which join should be used?
Answer: A Non-Equi Join, because the salary must be matched to a range of values rather than an exact value.
Example:
SELECT e.employee_name,
b.bonus_percent
FROM employees e
JOIN bonus_rules b
ON e.salary BETWEEN b.min_salary
AND b.max_salary;
Non-Equi Joins are best for range-based matching, such as salary grades, tax slabs, and interval-based business rules.
No comments:
Post a Comment