CROSS JOIN cartesian product in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a CROSS JOIN in SQL, it combines every row from one table with every row from another. This can create a lot of results.
We want to understand how the work grows as the tables get bigger.
Analyze the time complexity of the following code snippet.
SELECT *
FROM Products
CROSS JOIN Colors;
This query pairs each product with every color, creating all possible combinations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Combining each row from Products with each row from Colors.
- How many times: For every product row, the query pairs it with all color rows.
As the number of products or colors grows, the total pairs grow quickly.
| Input Size (Products x Colors) | Approx. Operations |
|---|---|
| 10 x 5 | 50 |
| 100 x 50 | 5,000 |
| 1,000 x 500 | 500,000 |
Pattern observation: The total work grows by multiplying the sizes of both tables.
Time Complexity: O(n * m)
This means the work grows by multiplying the number of rows in the first table (n) by the number of rows in the second table (m).
[X] Wrong: "The CROSS JOIN only adds a small number of rows, so it's almost like a simple list."
[OK] Correct: Actually, the number of rows grows very fast because every row from one table pairs with every row from the other, making the result much larger than either table alone.
Understanding how CROSS JOIN scales helps you explain query costs clearly and shows you can think about how data size affects performance.
"What if we replaced CROSS JOIN with INNER JOIN on a condition that matches fewer rows? How would the time complexity change?"
Practice
CROSS JOIN do in SQL?Solution
Step 1: Understand the definition of CROSS JOIN
A CROSS JOIN combines every row from the first table with every row from the second table, creating all possible pairs.Step 2: Compare with other join types
Unlike INNER JOIN or LEFT JOIN, CROSS JOIN does not require a matching condition and returns the Cartesian product.Final Answer:
It returns all possible combinations of rows from two tables. -> Option AQuick Check:
CROSS JOIN = all combinations [OK]
- Confusing CROSS JOIN with INNER JOIN
- Thinking CROSS JOIN filters rows
- Assuming CROSS JOIN deletes or updates data
Employees and Departments?Solution
Step 1: Identify correct CROSS JOIN syntax
The correct syntax uses the keyword CROSS JOIN between two table names without any ON condition.Step 2: Eliminate incorrect options
SELECT * FROM Employees JOIN Departments ON Employees.id = Departments.id; uses JOIN with ON condition (not CROSS JOIN). SELECT * FROM Employees INNER JOIN Departments; uses INNER JOIN without ON, which is invalid. SELECT * FROM Employees WHERE CROSS JOIN Departments; uses WHERE incorrectly.Final Answer:
SELECT * FROM Employees CROSS JOIN Departments; -> Option CQuick Check:
Correct CROSS JOIN syntax = SELECT * FROM Employees CROSS JOIN Departments; [OK]
- Using JOIN without ON for CROSS JOIN
- Trying to use WHERE for joining tables
- Confusing INNER JOIN with CROSS JOIN syntax
Colors with rows: Red, BlueShapes with rows: Circle, Square, TriangleWhat will be the result of:
SELECT Colors.color, Shapes.shape FROM Colors CROSS JOIN Shapes;Solution
Step 1: Count rows in each table
Colors has 2 rows, Shapes has 3 rows.Step 2: Calculate CROSS JOIN result
CROSS JOIN returns all pairs: 2 * 3 = 6 rows combining each color with each shape.Final Answer:
[('Red', 'Circle'), ('Red', 'Square'), ('Red', 'Triangle'), ('Blue', 'Circle'), ('Blue', 'Square'), ('Blue', 'Triangle')] -> Option AQuick Check:
2 colors * 3 shapes = 6 pairs [OK]
- Listing only matching pairs instead of all combinations
- Confusing CROSS JOIN with INNER JOIN
- Ignoring total number of rows in result
SELECT * FROM A CROSS JOIN B WHERE A.id = B.id;What is the main issue with this query?
Solution
Step 1: Understand CROSS JOIN with WHERE filter
CROSS JOIN creates all pairs, then WHERE filters pairs where A.id = B.id.Step 2: Compare with INNER JOIN
This is equivalent to INNER JOIN on A.id = B.id but less efficient because CROSS JOIN generates many unnecessary pairs first.Final Answer:
It produces the same result as INNER JOIN but less efficiently. -> Option DQuick Check:
CROSS JOIN + WHERE = INNER JOIN behavior [OK]
- Thinking WHERE is invalid with CROSS JOIN
- Assuming CROSS JOIN filters rows automatically
- Confusing SELECT with UPDATE statements
Products with 4 rows and Colors with 5 rows.You want to generate a list of all product-color combinations but exclude combinations where the product is discontinued.
Which query correctly uses CROSS JOIN and filtering?
Solution
Step 1: Use CROSS JOIN to get all combinations
Use CROSS JOIN between Products and Colors to get every product-color pair.Step 2: Filter out discontinued products
Apply WHERE clause to keep only products where discontinued = FALSE.Step 3: Check other options for errors
SELECT p.name, c.color FROM Products p INNER JOIN Colors c ON p.discontinued = FALSE; uses INNER JOIN incorrectly with ON condition unrelated to join keys. SELECT p.name, c.color FROM Products p CROSS JOIN Colors c ON p.discontinued = FALSE; uses ON with CROSS JOIN, which is invalid syntax. SELECT p.name, c.color FROM Products p, Colors c WHERE p.discontinued = TRUE; filters discontinued = TRUE, opposite of requirement.Final Answer:
SELECT p.name, c.color FROM Products p CROSS JOIN Colors c WHERE p.discontinued = FALSE; -> Option BQuick Check:
CROSS JOIN + WHERE filters correctly [OK]
- Using ON clause with CROSS JOIN
- Filtering with wrong discontinued value
- Confusing INNER JOIN with CROSS JOIN usage
