CROSS JOIN
5. CROSS JOIN
Section titled “5. CROSS JOIN”What it does
Section titled “What it does”Produces the Cartesian product — every row in the left table is paired with every row in the right table. There is no ON condition. With 4 employees and 3 departments, the result is 4 × 3 = 12 rows.
Visual Diagram
Section titled “Visual Diagram”SELECT e.name, d.dept_nameFROM employees eCROSS JOIN departments d;Output size rule
Section titled “Output size rule”rows_in_A × rows_in_B = output rows
Warning: On large tables, CROSS JOINs are extremely expensive. A table of 1,000 rows crossed with another 1,000-row table yields 1,000,000 rows.
Real-world use cases
Section titled “Real-world use cases”- Generating all possible combinations (e.g., size × color for product variants)
- Creating test data
- Building a calendar — cross joining a list of dates with a list of time slots