Sub topic 11
Self Joins and Non-Equi Joins
Assume you know SELECT, WHERE, ORDER BY, primary and foreign keys, table aliases, and NULL. A self join uses one table twice in the same query. A non-equi join matches rows with an operator other than =.
Self joins: relating rows in one table
An employee table often stores a manager's employee ID in manager_id. That value refers back to employee_id in the same table. To display both names, join employees to itself.
Each reference needs a different alias. In this query, e represents the employee and m represents the manager:
SELECT e.last_name AS employee,
m.last_name AS manager
FROM employees e
LEFT OUTER JOIN employees m
ON e.manager_id = m.employee_id
ORDER BY e.last_name;The join condition compares the employee's manager_id with another row's employee_id. The LEFT OUTER JOIN keeps employees who have no manager. For those rows, m.last_name is NULL. This is common for the highest-level employee in an organization.
An inner join would return only employees with a matching manager:
SELECT e.last_name AS employee,
m.last_name AS manager
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id;The two aliases are essential. Without them, Oracle cannot tell which occurrence of last_name, employee_id, or manager_id you mean.
Non-equi joins: matching ranges
A non-equi join uses conditions such as <, <=, >, >=, or BETWEEN. It is useful when one table defines ranges and another table supplies values.
The following query assigns each employee to a salary band. The inline view creates three bands for the example:
SELECT e.last_name,
e.salary,
g.grade
FROM employees e
JOIN (
SELECT 1 AS grade, 0 AS lower_salary, 5000 AS upper_salary
FROM dual
UNION ALL
SELECT 2, 5001, 10000
FROM dual
UNION ALL
SELECT 3, 10001, 20000
FROM dual
) g
ON e.salary BETWEEN g.lower_salary AND g.upper_salary
ORDER BY e.salary;BETWEEN includes both endpoints. Therefore, a salary of 5000 belongs to grade 1, while 5001 belongs to grade 2.
A range table must be designed carefully. If two ranges overlap, one employee can match both ranges, producing multiple result rows. If a value falls into no range, an inner join omits it.
You can use a left join when every employee must appear, even when no band matches:
SELECT e.last_name,
e.salary,
g.grade
FROM employees e
LEFT OUTER JOIN salary_grades g
ON e.salary BETWEEN g.lower_salary AND g.upper_salary;Here, salary_grades represents a real table with columns for the lower and upper limits. Employees outside every defined range remain in the result with a NULL grade.
| Join condition | Typical use |
|---|---|
| `a.id = b.id` | Matching related keys |
| `a.value BETWEEN b.low AND b.high` | Assigning a value to a range |
| `a.start_date < b.end_date` | Finding overlapping periods |
Common errors
- Give every table occurrence an alias and qualify shared column names.
- Check that non-equi ranges do not overlap or leave unintended gaps.
- Remember that a
NULLmanager ID cannot equal any employee ID. - Choose an outer join when unmatched rows still matter.
- Test whether the relationship is one-to-one, one-to-many, or many-to-many. That relationship determines whether rows multiply.
Exercise
Which employees appear in the self join when the query uses LEFT OUTER JOIN and an employee has a NULL manager_id?
Show answer
The employee still appears. Columns selected from the manager alias contain NULL because no manager row matches.