0
0
SQLquery~10 mins

CROSS JOIN cartesian product in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CROSS JOIN cartesian product
Start with Table A
Start with Table B
Pair each row of A with each row of B
Create combined rows for every pair
Result: All possible combinations
End
CROSS JOIN pairs every row from the first table with every row from the second table, creating all possible combinations.
Execution Sample
SQL
SELECT * FROM Colors CROSS JOIN Shapes;
This query combines every color with every shape, producing all color-shape pairs.
Execution Table
StepActionColors RowShapes RowOutput Row
1Take first row from ColorsRed--
2Pair with first row from ShapesRedCircle(Red, Circle)
3Pair with second row from ShapesRedSquare(Red, Square)
4Pair with third row from ShapesRedTriangle(Red, Triangle)
5Take second row from ColorsBlue--
6Pair with first row from ShapesBlueCircle(Blue, Circle)
7Pair with second row from ShapesBlueSquare(Blue, Square)
8Pair with third row from ShapesBlueTriangle(Blue, Triangle)
9Take third row from ColorsGreen--
10Pair with first row from ShapesGreenCircle(Green, Circle)
11Pair with second row from ShapesGreenSquare(Green, Square)
12Pair with third row from ShapesGreenTriangle(Green, Triangle)
13No more rows in Colors--Stop
💡 All rows from Colors paired with all rows from Shapes, no more rows left in Colors.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11After 12Final
Colors RowNoneRedRedRedRedBlueBlueBlueBlueGreenGreenGreenGreenNone
Shapes RowNoneNoneCircleSquareTriangleNoneCircleSquareTriangleNoneCircleSquareTriangleNone
Output RowNoneNone(Red, Circle)(Red, Square)(Red, Triangle)None(Blue, Circle)(Blue, Square)(Blue, Triangle)None(Green, Circle)(Green, Square)(Green, Triangle)None
Key Moments - 3 Insights
Why does the number of output rows equal the product of rows in both tables?
Because each row in the first table pairs with every row in the second table, as shown in execution_table rows 2-4 for the first Colors row.
Is there any filtering or matching condition in CROSS JOIN?
No, CROSS JOIN pairs all rows without any condition, which is why every combination appears in the output as seen in all pairing steps.
What happens if one table is empty?
If either table has zero rows, the CROSS JOIN result is empty because there are no rows to pair, which would stop execution early (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output row at step 7?
A(Red, Square)
B(Blue, Square)
C(Green, Circle)
D(Blue, Triangle)
💡 Hint
Check the Output Row column at step 7 in the execution_table.
At which step does the CROSS JOIN start pairing rows from the second Colors row?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the Colors Row column to see when it changes from 'Red' to 'Blue'.
If the Shapes table had only 2 rows, how many output rows would there be for 3 Colors rows?
A6
B5
C3
D2
💡 Hint
Multiply the number of rows in Colors by the number of rows in Shapes.
Concept Snapshot
CROSS JOIN syntax: SELECT * FROM table1 CROSS JOIN table2;
It pairs every row of table1 with every row of table2.
Result rows = rows in table1 × rows in table2.
No filtering or matching conditions applied.
Useful for generating all combinations.
Full Transcript
CROSS JOIN creates a Cartesian product of two tables by pairing each row from the first table with every row from the second table. For example, if Colors has 3 rows and Shapes has 3 rows, the result will have 9 rows. The execution steps show how each Colors row is paired with all Shapes rows one by one. This join does not filter or match rows; it simply combines all possible pairs. If either table is empty, the result is empty. This is useful when you want to generate all combinations of two sets of data.