QA
Beginner LevelSQL & MySQL

What is a CROSS JOIN and when does it produce a Cartesian Product?

❓ Interview Prompt: Explain `CROSS JOIN`, Cartesian Products ($N \times M$ row combinations), valid use cases, and accidental cross joins.

A 'CROSS JOIN' produces a Cartesian Product combining every row from the first table with every row from the second table:

Row Count Formula:

$$\text{Total Rows} = \text{Rows(Table A)} \times \text{Rows(Table B)}$$

•
Valid Use Case: Generating matrix combinations (e.g. pairing all product sizes with all color variations).
•
⚠️ Accidental Cartesian Product: Forgetting join conditions in standard comma-separated table joins ('SELECT * FROM tableA, tableB').

Code Implementation Example

sql
-- Generating all product size and color combinations
SELECT s.size_name, c.color_name
FROM sizes s
CROSS JOIN colors c;
💡Generating matrix permutations using CROSS JOIN.
🎯 Interview Key Takeaway

Use CROSS JOIN intentionally for generating matrix permutations, and ensure standard joins include explicit ON conditions.

#SQL#CROSS JOIN#Cartesian Product#Beginner
Back to all SQL & MySQL Questions→

More SQL & MySQL Interview Questions

Beginner LevelSQL & MySQL

How do B-Tree Indexes work in SQL/MySQL and how do you optimize slow queries using EXPLAIN?

Beginner LevelSQL & MySQL

How do SELECT, WHERE, ORDER BY, and LIMIT clauses filter relational datasets?

Beginner LevelSQL & MySQL

What are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN?