15. FULL OUTER JOIN & CROSS JOIN
Level: Guided beginner
- FULL OUTER JOIN -- all rows from both tables
- NULL in both left and right sides
- When to use FULL OUTER JOIN
- CROSS JOIN -- Cartesian product
- Cross join use cases (combinations, permutations)
- Performance implications
- Common FULL/CROSS JOIN mistakes
INNER JOIN = matching only. LEFT/RIGHT JOIN = one side complete. FULL OUTER JOIN = both sides complete. CROSS JOIN = every combination. This article covers rare but useful JOIN types.
Why FULL OUTER & CROSS JOIN Matter
FULL OUTER: Report unmatched on both sides:
Teachers without subjects + Subjects without teachers
CROSS JOIN: Generate combinations:
All possible (Student, Exam) pairs for scheduling
FULL OUTER JOIN Syntax
SELECT *
FROM Table1
FULL OUTER JOIN Table2
ON Table1.ForeignKey = Table2.PrimaryKey;
Effect: All rows from both tables. NULLs where no match.
Example 1: Teachers + Subjects (both sides)
SELECT
t.Name AS Teacher,
s.Name AS Subject
FROM Teacher t
FULL OUTER JOIN Subject s ON t.TeacherId = s.TeacherId;
Result (all teachers AND all subjects):
Teacher | Subject
Dr. Mehta | Maths
Dr. Mehta | Science
Mrs. Rao | English
Mr. Sharma | NULL <- Teacher without subject
NULL | Hindi <- Subject without teacher
Mr. Sharma (teacher without subject) AND Hindi (subject without teacher).
Comparison: INNER vs LEFT vs FULL
Teacher table: Subject table:
1 Dr. Mehta 10 Maths (1)
2 Mrs. Rao 11 Science (1)
3 Mr. Sharma 12 English (2)
13 Hindi (99 - no teacher)
INNER JOIN:
Dr. Mehta | Maths
Dr. Mehta | Science
Mrs. Rao | English
(Mr. Sharma, Hindi excluded)
LEFT JOIN:
Dr. Mehta | Maths
Dr. Mehta | Science
Mrs. Rao | English
Mr. Sharma | NULL
(Hindi excluded)
FULL OUTER JOIN:
Dr. Mehta | Maths
Dr. Mehta | Science
Mrs. Rao | English
Mr. Sharma | NULL
NULL | Hindi
(Both unmatched included)
CROSS JOIN Syntax
SELECT *
FROM Table1
CROSS JOIN Table2;
Effect: Every row from Table1 x Every row from Table2 (Cartesian product).
CROSS JOIN of 1000 rows x 1000 rows = 1,000,000 rows. Use carefully!
Example 2: CROSS JOIN (Cartesian Product)
Create all (Student, Exam) combinations:
SELECT
s.Name AS Student,
e.ExamName
FROM Student s
CROSS JOIN Exam e;
Result (every student paired with every exam):
Student | ExamName
Ravi Kumar | Maths Final
Ravi Kumar | Science Final
Ravi Kumar | English Final
Priya Sharma | Maths Final
Priya Sharma | Science Final
Priya Sharma | English Final
Arjun Reddy | Maths Final
Arjun Reddy | Science Final
Arjun Reddy | English Final
3 students x 3 exams = 9 rows (all combinations).
CROSS JOIN Use Cases
1. Generate all possible combinations:
SELECT * FROM Size CROSS JOIN Color;
-- All shirt sizes x all colors (S, M, L) x (Red, Blue, White)
2. Create schedule grid:
SELECT * FROM TimeSlot CROSS JOIN Classroom;
-- All time slots x all classrooms (scheduling)
3. Product variations:
SELECT * FROM BaseProduct CROSS JOIN Feature;
-- All product x feature combinations
Example 3: CROSS JOIN with WHERE (filtered)
Create schedule for students and exams (filtered):
SELECT
s.Name AS Student,
e.ExamName,
ROW_NUMBER() OVER (ORDER BY s.StudentId, e.ExamId) AS SeatNumber
FROM Student s
CROSS JOIN Exam e
WHERE s.ClassName = '10-A'
ORDER BY s.Name, e.ExamName;
Result (10-A students x exams, with seat numbers):
Student | ExamName | SeatNumber
Ravi Kumar | Maths Final | 1
Ravi Kumar | Science Final | 2
Priya Sharma | Maths Final | 3
Priya Sharma | Science Final | 4
FULL OUTER JOIN to Find Unmatched on Both Sides
Find unassigned teachers AND subjects:
SELECT
t.Name AS Teacher,
s.Name AS Subject
FROM Teacher t
FULL OUTER JOIN Subject s ON t.TeacherId = s.TeacherId
WHERE t.TeacherId IS NULL OR s.SubjectId IS NULL;
Result (only unmatched):
Teacher | Subject
Mr. Sharma | NULL <- Teacher without subject
NULL | Hindi <- Subject without teacher
Common FULL/CROSS JOIN Mistakes
Mistake 1: CROSS JOIN with huge tables
Wrong:
SELECT * FROM Orders o
CROSS JOIN Customers c;
-- 1M orders x 100K customers = 100 BILLION rows!
Result: System hangs/crashes. Out of memory.
Fix: Use proper JOIN:
SELECT * FROM Orders o
INNER JOIN Customers c ON o.CustomerId = c.CustomerId;
Mistake 2: Forgetting WHERE in CROSS JOIN
Wrong:
SELECT * FROM Product p
CROSS JOIN Feature f;
-- All products x all features (unnecessary rows)
Result: Too much data, performance issues.
Fix: Filter with WHERE:
SELECT * FROM Product p
CROSS JOIN Feature f
WHERE p.CategoryId = f.CategoryId;
-- Only relevant combinations
Mistake 3: Using FULL OUTER instead of LEFT/RIGHT
Wrong:
SELECT * FROM Teacher t
FULL OUTER JOIN Subject s
WHERE s.SubjectId IS NULL;
-- Unintentionally filters results
Result: Same as LEFT JOIN with WHERE (confusing).
Fix: Use LEFT JOIN if you want only unmatched left:
SELECT * FROM Teacher t
LEFT JOIN Subject s ON t.TeacherId = s.TeacherId
WHERE s.SubjectId IS NULL;
Mistake 4: Not considering performance of CROSS JOIN
Wrong:
SELECT * FROM Student CROSS JOIN Exam CROSS JOIN Subject;
-- 100 students x 50 exams x 20 subjects = 100,000 rows
-- For scheduling/reporting, OK but verify
Result: Slow queries, high resource usage.
Fix: Test with WHERE filters and indexes.
Best Practices for FULL/CROSS JOIN
- FULL OUTER JOIN rarely needed -- Check LEFT+RIGHT union instead
- CROSS JOIN requires care -- Always filter, check result size
- Use CROSS JOIN for combinations -- Scheduling, product variants
- Test row counts -- Understand result set size before running
- Index carefully -- CROSS JOIN can't be optimized much
- Document intent -- Comment explains why CROSS JOIN used
- Prefer UNION over FULL OUTER -- Sometimes clearer
FULL OUTER JOIN returns all rows from both tables:
Teacher t FULL OUTER JOIN Subject s
All teachers (matched or not) +
All subjects (matched or not) +
NULLs for unmatched pairs
Result includes:
- Matched rows (Dr. Mehta with Maths)
- Unmatched from left (Mr. Sharma with NULL subject)
- Unmatched from right (NULL teacher with Hindi)
Use when: Need unmatched rows from BOTH sides.
Example:
-- Find teachers without subjects AND subjects without teachers
SELECT * FROM Teacher t
FULL OUTER JOIN Subject s
WHERE t.TeacherId IS NULL OR s.SubjectId IS NULL;
Alternative (often clearer):
SELECT * FROM Teacher WHERE TeacherId NOT IN (SELECT TeacherId FROM Subject)
UNION
SELECT * FROM Subject WHERE SubjectId NOT IN (SELECT SubjectId WHERE...)
UNION sometimes clearer than FULL OUTER.
Cartesian product = every row paired with every other row:
3 students CROSS JOIN 5 exams = 15 combinations
Use for:
- Schedule generation (student x time slot)
- Product variants (size x color)
- All combinations report
Warning: Can create huge results (100K x 100K = 10B rows).
Small tables = OK:
5 Sizes CROSS JOIN 3 Colors = 15 rows (OK)
Large tables = disaster:
1M Orders CROSS JOIN 100K Customers = 100B rows (CRASH)
Rule: Always check table sizes before CROSS JOIN.
Yes, and should:
SELECT * FROM Product p
CROSS JOIN Feature f
WHERE p.CategoryId = f.CategoryId;
-- Only relevant combinations, not all
WHERE filters the Cartesian product (essential for performance).
FULL OUTER JOIN:
SELECT * FROM Teacher t
FULL OUTER JOIN Subject s
WHERE t.TeacherId IS NULL OR s.SubjectId IS NULL;
UNION (often clearer):
SELECT t.* FROM Teacher t
LEFT JOIN Subject s ON t.TeacherId = s.TeacherId
WHERE s.SubjectId IS NULL
UNION
SELECT s.* FROM Subject s
LEFT JOIN Teacher t ON s.TeacherId = t.TeacherId
WHERE t.TeacherId IS NULL;
UNION: More explicit about what's unmatched. Choose based on readability.
Use ChatGPT, Claude, or Copilot to go deeper on FULL OUTER JOIN and CROSS JOIN in SQL Server. Try these prompts:
"Explain FULL OUTER JOIN - show matched, unmatched left, unmatched right""What's a Cartesian product? Show CROSS JOIN example""When should I use CROSS JOIN? When avoid it?""How do I find unmatched on both sides - FULL OUTER vs UNION?""Quiz me: ask 5 questions about FULL OUTER, CROSS JOIN, Cartesian products"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.