Sub topic 12
Cross Joins and Cartesian Products
This chapter assumes that you know basic SELECT, WHERE, and ORDER BY, table aliases, primary and foreign keys, and the meaning of NULL.
What a cross join does
A cross join combines every row from one table with every row from another table. The result is called a Cartesian product.
If the first table has m rows and the second table has n rows, the result has m × n row combinations.
For example, if departments has 4 rows and employees has 20 rows, a cross join produces 80 combinations. Most combinations will not represent a real employee-to-department relationship.
SELECT d.department_name, e.last_name
FROM departments d
CROSS JOIN employees e;This query intentionally creates every possible department and employee pair. It does not compare department_id values.
| Departments | Employees | Result rows |
|---|---|---|
| 4 | 20 | 80 |
A cross join can be useful when you need every combination. For example, you might create a planning grid containing every product and every month. In ordinary relationship queries, however, an unrestricted cross join is usually a mistake.
How accidental Cartesian products happen
The older comma-separated syntax lists tables in the FROM clause. The relationship between those tables must then appear in the WHERE clause.
This query is an inner join:
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;If the condition is omitted, Oracle returns a Cartesian product:
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e, departments d;The same problem can occur with ANSI syntax when you write CROSS JOIN instead of a relationship join:
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e
CROSS JOIN departments d;With ANSI joins, a normal relationship should usually be written with JOIN ... ON. The ON clause makes the required matching condition visible beside the join.
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;Cross join followed by a filter
A cross join followed by a WHERE condition can produce the same final rows as an inner join:
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e
CROSS JOIN departments d
WHERE e.department_id = d.department_id;Logically, Oracle can treat this as matching rows by department_id. However, JOIN ... ON communicates the intent more clearly and reduces the chance that a future edit will remove or weaken the condition.
The difference is especially important when several tables are involved. Each relationship should have its own ON clause.
Finding row multiplication
A join can also multiply rows even when it has a condition. This happens when one row on either side matches several rows on the other side.
For example, one department can match many employees. A department therefore appears once for each matching employee. That is expected for a department-to-employees detail report, but it may be wrong if you wanted one row per department.
Use aggregation or DISTINCT only after deciding what one output row should represent. DISTINCT can hide a faulty join rather than fix it.
A practical check
Before executing a multi-table query, check:
- Does every table have a deliberate relationship to another table?
- Is the relationship condition in the correct
ONorWHEREclause? - Could one matching row legitimately produce several output rows?
- Are
NULLjoin keys expected to produce no match? - Is a cross join explicitly required?
Exercise
departments has 10 rows and employees has 107 rows. How many rows can an unrestricted cross join produce, and what change makes a department-to-employee query an inner join?
Show answer
It can produce 1,070 rows. Add the relationship condition e.department_id = d.department_id, preferably with JOIN departments d ON e.department_id = d.department_id.