CROSS JOIN behavior in PostgreSQL - Time & Space Complexity
When using CROSS JOIN in SQL, it combines every row from one table with every row from another. Understanding how this grows helps us know how long queries might take as tables get bigger.
We want to see how the work done increases when the tables have more rows.
Analyze the time complexity of the following code snippet.
SELECT a.id, b.name
FROM table_a a
CROSS JOIN table_b b;
This query pairs every row from table_a with every row from table_b, producing all possible combinations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Combining each row of
table_awith each row oftable_b. - How many times: For every row in
table_a, the operation repeats for every row intable_b.
As the number of rows in each table grows, the total pairs grow much faster.
| Input Size (rows in each table) | Approx. Output Rows |
|---|---|
| 10 | 10 x 10 = 100 |
| 100 | 100 x 100 = 10,000 |
| 1000 | 1000 x 1000 = 1,000,000 |
Pattern observation: The total combinations grow by multiplying the sizes, so doubling the size of both tables makes the work four times bigger.
Time Complexity: O(n * m)
This means the work grows by multiplying the number of rows in the first table by the number of rows in the second table.
[X] Wrong: "The time grows just like the bigger table's size."
[OK] Correct: Because CROSS JOIN pairs every row from one table with every row from the other, the total work depends on both tables multiplied, not just the bigger one.
Knowing how CROSS JOIN scales helps you explain query performance clearly and shows you understand how databases handle combining data. This skill is useful when discussing query design and optimization.
"What if one table has an index or filter applied before the CROSS JOIN? How would that affect the time complexity?"
Practice
CROSS JOIN do in PostgreSQL?Solution
Step 1: Understand CROSS JOIN concept
A CROSS JOIN creates combinations of all rows from both tables without any condition.Step 2: Compare with other JOIN types
Unlike INNER JOIN or LEFT JOIN, CROSS JOIN does not filter or match rows but pairs all possible combinations.Final Answer:
Pairs every row from the first table with every row from the second table. -> Option AQuick Check:
CROSS JOIN = all combinations [OK]
- Confusing CROSS JOIN with INNER JOIN
- Thinking CROSS JOIN filters rows
- Assuming CROSS JOIN requires a join condition
Solution
Step 1: Recall CROSS JOIN syntax
The correct syntax isSELECT * FROM table1 CROSS JOIN table2;without any ON clause.Step 2: Identify incorrect options
SELECT * FROM table1 JOIN table2 ON table1.id = table2.id; uses INNER JOIN with ON clause, C has invalid keyword INNER CROSS JOIN, A has misplaced ON keyword.Final Answer:
SELECT * FROM table1 CROSS JOIN table2; -> Option AQuick Check:
Syntax for CROSS JOIN = SELECT * FROM table1 CROSS JOIN table2; [OK]
- Adding ON clause with CROSS JOIN
- Using INNER CROSS JOIN which is invalid
- Misplacing keywords in the query
Colors:color_id | color_name
1 | Red
2 | Blue
Shapes:shape_id | shape_name
1 | Circle
2 | Square
3 | Triangle
How many rows will this query return?
SELECT color_name, shape_name 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 size
Result rows = 2 x 3 = 6 rows, all combinations of colors and shapes.Final Answer:
6 -> Option DQuick Check:
2 x 3 = 6 rows [OK]
- Adding rows instead of multiplying
- Ignoring one table's row count
- Confusing CROSS JOIN with INNER JOIN
Table A:id | name
1 | Alice
2 | Bob
Table B:code
X
Y
What is the result of:
SELECT A.name, B.code FROM A CROSS JOIN B;Solution
Step 1: Understand CROSS JOIN output
Each row in A pairs with every row in B, so 2 rows in A x 2 rows in B = 4 rows total.Step 2: List all combinations
Pairs are (Alice, X), (Alice, Y), (Bob, X), (Bob, Y) in that order.Final Answer:
[('Alice', 'X'), ('Alice', 'Y'), ('Bob', 'X'), ('Bob', 'Y')] -> Option BQuick Check:
2 x 2 = 4 rows [OK]
- Assuming CROSS JOIN filters rows
- Mixing order of pairs incorrectly
- Counting only matching rows instead of all combinations
SELECT * FROM table1 CROSS JOIN table2 ON table1.id = table2.id;What is the problem with this query?
Solution
Step 1: Check CROSS JOIN syntax rules
CROSS JOIN syntax does not support ON clause; adding ON causes syntax error.Step 2: Understand error cause
ON clause is used with INNER or LEFT JOIN, not CROSS JOIN.Final Answer:
Syntax error because CROSS JOIN does not allow ON clause. -> Option CQuick Check:
CROSS JOIN + ON = syntax error [OK]
- Thinking CROSS JOIN can have ON clause
- Confusing CROSS JOIN with INNER JOIN
- Expecting filtered results from CROSS JOIN
Ready to prove it?
Pass the test and this pattern moves unknown
