Sub topic 1
What a Join Does
Prerequisites
This chapter assumes that you can write basic SELECT, WHERE, and ORDER BY clauses. You should also know primary keys, foreign keys, table aliases, and the meaning of NULL.
Combining rows from related tables
A join combines rows from two or more tables by applying a relationship between their columns. Usually, one table stores a primary key and another stores the same value as a foreign key.
For example, an employees table can store department_id, while a departments table uses department_id as its primary key. The value connects each employee to a department.
| employees.employee_id | employees.last_name | employees.department_id | departments.department_name |
|---|---|---|---|
| 101 | King | 10 | Sales |
| 102 | Chen | 20 | Finance |
A join does not permanently merge the tables. It creates a result set for one query. Each result row contains selected columns from the matching source rows.
The join condition
A join condition states which rows belong together. In this example, the condition compares the department ID in both tables:
SELECT e.employee_id,
e.last_name,
d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;e and d are table aliases. An alias is a short name for a table within one statement. The condition after ON says that an employee matches a department when both department_id values are equal.
This query uses an inner join. An inner join returns only rows for which the condition is true. An employee with a NULL department ID, or with an ID that does not exist in departments, has no matching department and is omitted.
A join can use columns with different names if their values represent the same relationship:
SELECT o.order_id,
c.customer_name
FROM orders o
JOIN customers c
ON o.customer_number = c.customer_id;The columns do not need matching names. They need compatible values and a meaningful relationship.
How Oracle finds matches
Conceptually, Oracle compares candidate rows and keeps the combinations that satisfy the join condition. If one department has three employees, that department can appear in three result rows, once for each employee.
The result is therefore shaped by the relationship:
- One-to-one relationships usually produce at most one result row per source row.
- One-to-many relationships repeat the one-side values for each matching many-side row.
- A missing condition can combine every row from one table with every row from another table.
The older comma syntax
Oracle also supports an older style that lists tables after FROM and puts the join condition in WHERE:
SELECT e.employee_id,
e.last_name,
d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;For this simple inner join, the result is equivalent to the ANSI JOIN ... ON version. ANSI syntax makes the relationship easier to see and separates join logic from other filters, so it is generally preferred for new code.
For example, a salary filter can be kept separate:
SELECT e.last_name,
d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
WHERE e.salary > 10000;The ON clause describes how rows from tables match. The WHERE clause filters the rows produced by that match. This distinction becomes especially important with outer joins.
A missing condition
When two tables are listed without a join condition, every row from the first table can pair with every row from the second. This result is a Cartesian product, also called a cross join. If one table has 4 rows and another has 3, the result has 12 combinations.
This behavior is sometimes intentional, but it is often an accidental missing condition. Check every multi-table query for a clear relationship between its tables.
Exercise
An employee has departmentid 20, but no row in departments has departmentid 20. Will the inner join query return that employee?
Show answer
No. An inner join returns only rows with a matching department. The employee is omitted unless the query uses an outer join that preserves unmatched employees.