Table of Contents
- Introduction
- What is a FULL OUTER JOIN?
- Syntax of FULL OUTER JOIN
- When to Use FULL OUTER JOIN
- FULL OUTER JOIN Example
- Filtering with FULL OUTER JOIN
- Handling NULLs in FULL OUTER JOIN
- Emulating FULL OUTER JOIN in MySQL/SQLite
- What is a CROSS JOIN?
- Syntax of CROSS JOIN
- When to Use CROSS JOIN
- CROSS JOIN Example
- CROSS JOIN vs INNER JOIN
- Cartesian Product Explained
- Combining CROSS JOIN with Filters
- Real-World Example: FULL OUTER JOIN
- Real-World Example: CROSS JOIN
- Performance Considerations
- Common Mistakes with OUTER and CROSS JOIN
- Summary and What’s Next
1. Introduction
Two advanced SQL join types — FULL OUTER JOIN
and CROSS JOIN
— help retrieve either complete or combinatorially exhaustive datasets. In this module, you’ll learn when and how to use them, their performance implications, and real-world relevance.
2. What is a FULL OUTER JOIN?
A FULL OUTER JOIN
returns all rows from both tables. If there’s no match, the missing side is filled with NULL
.
3. Syntax of FULL OUTER JOIN
sqlCopyEditSELECT a.column, b.column
FROM tableA a
FULL OUTER JOIN tableB b
ON a.id = b.a_id;
This includes:
- Rows with matching values
- Rows from
tableA
without match intableB
- Rows from
tableB
without match intableA
4. When to Use FULL OUTER JOIN
Use it when:
- You want to retain all data from both tables
- You need to analyze unmatched records on both sides
- You’re building data comparison or auditing reports
5. FULL OUTER JOIN Example
students
id | name |
---|---|
1 | Ravi |
2 | Sneha |
library_users
id | student_id |
---|---|
1 | 2 |
2 | 3 |
sqlCopyEditSELECT s.name, l.student_id
FROM students s
FULL OUTER JOIN library_users l
ON s.id = l.student_id;
Result:
name | student_id |
---|---|
Ravi | NULL |
Sneha | 2 |
NULL | 3 |
6. Filtering with FULL OUTER JOIN
To find unmatched rows only:
sqlCopyEditSELECT *
FROM tableA a
FULL OUTER JOIN tableB b ON a.id = b.id
WHERE a.id IS NULL OR b.id IS NULL;
Useful for auditing mismatches between datasets.
7. Handling NULLs in FULL OUTER JOIN
Expect NULL
in columns where no match occurs.
Use COALESCE()
to default values:
sqlCopyEditSELECT COALESCE(a.name, b.name) AS entity_name
FROM a
FULL OUTER JOIN b ON a.id = b.id;
8. Emulating FULL OUTER JOIN in MySQL/SQLite
Since MySQL and SQLite don’t support FULL OUTER JOIN natively, emulate it using UNION
:
sqlCopyEditSELECT * FROM A
LEFT JOIN B ON A.id = B.id
UNION
SELECT * FROM A
RIGHT JOIN B ON A.id = B.id;
9. What is a CROSS JOIN?
A CROSS JOIN
returns the Cartesian product of two tables — every row from table A is paired with every row from table B.
10. Syntax of CROSS JOIN
sqlCopyEditSELECT a.column, b.column
FROM tableA a
CROSS JOIN tableB b;
OR:
sqlCopyEditSELECT a.column, b.column
FROM tableA a, tableB b;
11. When to Use CROSS JOIN
- To generate all possible combinations
- For matrix-style reports (e.g., regions × quarters)
- For calendar creation, shift schedules, or test datasets
12. CROSS JOIN Example
colors
color |
---|
Red |
Blue |
sizes
size |
---|
S |
M |
sqlCopyEditSELECT color, size
FROM colors
CROSS JOIN sizes;
Result:
color | size |
---|---|
Red | S |
Red | M |
Blue | S |
Blue | M |
13. CROSS JOIN vs INNER JOIN
Feature | CROSS JOIN | INNER JOIN |
---|---|---|
Match logic | No matching condition | Requires matching condition |
Result size | Rows_A × Rows_B | Only matched rows |
Use case | Combinations or testing | Real relationships |
14. Cartesian Product Explained
If table A has 3 rows and table B has 4 rows:
- CROSS JOIN will return 3 × 4 = 12 rows
15. Combining CROSS JOIN with Filters
sqlCopyEditSELECT c.color, s.size
FROM colors c
CROSS JOIN sizes s
WHERE c.color != 'Red';
Generates combinations, but filters out unnecessary pairs.
16. Real-World Example: FULL OUTER JOIN
Find active and inactive product SKUs between two systems:
sqlCopyEditSELECT *
FROM systemA_products a
FULL OUTER JOIN systemB_products b
ON a.sku = b.sku
WHERE a.sku IS NULL OR b.sku IS NULL;
Helps with reconciliation reports.
17. Real-World Example: CROSS JOIN
Generate price list for all sizes of all products:
sqlCopyEditSELECT p.name, s.size
FROM products p
CROSS JOIN sizes s;
Use this for e-commerce variants, test data generation, etc.
18. Performance Considerations
Join Type | Rows Generated | Notes |
---|---|---|
FULL OUTER JOIN | Up to A + B | Slower on large datasets |
CROSS JOIN | A × B | Use cautiously with large sets |
Indexing improves FULL OUTER JOIN, but doesn’t apply to CROSS JOIN (no match condition).
19. Common Mistakes with OUTER and CROSS JOIN
- Misusing CROSS JOIN instead of INNER JOIN
- Forgetting to handle
NULL
in FULL OUTER JOIN - Overloading CROSS JOIN on large tables → memory spikes
- Not supporting FULL JOIN in DBMS (like MySQL, SQLite)
20. Summary and What’s Next
- FULL OUTER JOIN combines unmatched + matched data from both tables
- CROSS JOIN generates every possible row combination
- Use each judiciously for comparisons, reconciliations, and generating combinations