Joining tables in Oracle is governed by the Cost-Based Optimizer (CBO), which chooses between three underlying algorithmic mechanisms based on table statistics, data distribution, and memory availability.
The Three Physical Join Algorithms
Understanding these three algorithms enables you to read execution plans and design performant schemas:
| Join Algorithm | Best Use Case | Memory Usage | Requires Index? |
|---|---|---|---|
| Nested Loops | OLTP queries returning small subsets; driving table has few rows | Minimal (row-by-row) | Yes, on inner join key |
| Hash Join | Data warehousing; large equijoins (=) | High (builds hash table in PGA) | No |
| Sort-Merge Join | Non-equijoins (<, >, BETWEEN); inputs already sorted | Medium (sorting buffers) | Beneficial if pre-sorted |
1. Nested Loops Join Mechanics
For every row in the outer (driving) table, Oracle probes the inner table. If the driving table returns 5 rows and the inner table has a unique index on the join key, this executes in fractions of a millisecond. If the driving table returns 500,000 rows, a nested loop becomes a performance catastrophe.
2. Hash Join Mechanics
Oracle takes the smaller of the two datasets, hashes the join keys, and loads an in-memory hash table into the PGA. It then streams the larger table, hashes each probe row's join key, and checks for hash bucket matches.
-- Standard ANSI Equijoin optimized via Hash Join or Nested Loops
SELECT
e.employee_id,
e.first_name || ' ' || e.last_name AS employee_name,
d.department_name,
l.city,
c.country_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
INNER JOIN locations l ON d.location_id = l.location_id
INNER JOIN countries c ON l.country_id = c.country_id
WHERE e.salary > 80000;
ANSI Joins vs The Historical Oracle (+) Syntax
Before ANSI SQL-92 support was introduced, Oracle used the proprietary (+) operator to express outer joins. You will encounter this across enterprise legacy codebases:
-- ANSI Standard LEFT OUTER JOIN (Recommended)
SELECT e.last_name, d.department_name
FROM employees e
LEFT OUTER JOIN departments d ON e.department_id = d.department_id;
-- Legacy Oracle Outer Join equivalent:
-- The (+) sits on the side that produces NULLs when a match is absent
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+);
Rule: Never mix ANSI join syntax with the legacy
(+)syntax in the same query. It confuses the parser and can trigger unexpected Cartesian products.
Advanced Null Handling Functions
Oracle treats empty strings ('') as NULL. Handling null values cleanly is essential:
NVL(expr1, expr2): Returnsexpr2ifexpr1is null.NVL2(expr1, value_if_not_null, value_if_null): Three-argument ternary operator.COALESCE(e1, e2, ..., en): Standard short-circuiting null evaluation.NULLIF(expr1, expr2): ReturnsNULLif both expressions are equal; otherwise returnsexpr1.DECODE(col, s1, r1, s2, r2, default): Oracle's built-in branch evaluation function.
SELECT
product_name,
NVL(discount_price, list_price) AS effective_price,
NVL2(commission_pct, 'Commissioned Rep', 'Salaried Staff') AS compensation_model,
COALESCE(mobile_phone, office_phone, home_phone, 'No Contact Available') AS primary_phone
FROM sales_representatives;
Exercises
- Write a query performing a
FULL OUTER JOINbetweensuppliersandproductsto find unassigned products and suppliers with no listed inventory. - Explain why a Hash Join is fundamentally restricted to equijoins (
a.id = b.id) and cannot execute range joins (a.date BETWEEN b.start AND b.end). - Replace a complex
CASE WHENstatement with a cleanCOALESCEandNULLIFpipeline.