Sure thing!
In Oracle you’ve got two ways to write an outer-join:
- The modern, ANSI-SQL syntax (recommended)
- The old Oracle-specific “(+)" syntax (still supported, but fading out)
Below is a quick cheat-sheet for both, plus some tips.
1. ANSI-SQL Syntax (Oracle 9i+)
Use LEFT / RIGHT / FULL OUTER JOIN with an ON clause.
-- LEFT OUTER JOIN (keep all rows from A)
SELECT a.id,
a.name,
b.order_no
FROM customers a
LEFT OUTER JOIN orders b
ON b.customer_id = a.id;
-- RIGHT OUTER JOIN (keep all rows from B)
SELECT a.id,
a.name,
b.order_no
FROM customers a
RIGHT OUTER JOIN orders b
ON b.customer_id = a.id;
-- FULL OUTER JOIN (keep rows from both sides)
SELECT a.id,
a.name,
b.order_no
FROM customers a
FULL OUTER JOIN orders b
ON b.customer_id = a.id;
Key points
• LEFT = all rows from the left table, matching rows (or NULLs) from the right.
• RIGHT = mirror image.
• FULL = union of left + right; unmatched columns come back as NULL.
• You can chain extra predicates in the ON clause or later in WHERE.
2. Old “(+)" Oracle Syntax (pre-9i / legacy code)
-- same LEFT OUTER JOIN using (+)
SELECT a.id,
a.name,
b.order_no
FROM customers a,
orders b
WHERE b.customer_id(+) = a.id;
Rules:
• Put (+) on the column(s) from the optional side (the one that may be missing).
• You can’t outer-join both sides of the same equality.
• No FULL OUTER JOIN; only LEFT or RIGHT depending on which side gets (+).
• The syntax doesn’t mix well with other conditions, analytic functions, etc.
• Oracle says: please use ANSI style instead!
Why prefer ANSI style?
- Clearer & self-documenting
- Allows FULL OUTER JOIN
- Plays nicely with other ANSI features (e.g.,
JOIN … USING, cross joins, etc.)
- Easier to convert to/from other DB engines and modern ORMs.
- Oracle has optimized it just as well.
Practical Example with Filters
Suppose you want all customers and their 2023 orders (if any), but only for “ACTIVE” customers:
SELECT c.customer_id,
c.customer_name,
o.order_id,
o.order_date
FROM customers c
LEFT OUTER JOIN orders o
ON o.customer_id = c.customer_id
AND o.order_date BETWEEN DATE '2023-01-01' AND DATE '2023-12-31'
WHERE c.status = 'ACTIVE';
Notice:
• Filters that belong to the optional table (orders) go in the ON clause;
• Filters that belong to the mandatory table (customers) stay in WHERE.
Putting an outer-join column filter in the WHERE clause would accidentally switch it back to an INNER join.
Quick Troubleshooting Table
| Symptom | Likely Fix |
|---|
| Getting fewer rows than expected | Check if extra predicates accidentally landed in WHERE instead of ON. |
| ORA-00936 / ORA-01417 errors | Mixing old (+) with ANSI joins – pick one style only. |
| Want NULLs converted to something | Wrap columns: NVL(b.order_no, '— no order —'). |
TL;DR
SELECT …
FROM A
LEFT OUTER JOIN B ON (join_condition)
Put LEFT, RIGHT, or FULL as needed, keep optional-table filters inside the ON, and you’re golden. If you still see the mysterious (+) in old code, know that it means “this side is optional.”
Happy querying! 🚀