Cartesian product and joins in DBMS Theory - Time & Space Complexity
When working with database tables, it's important to know how the time to get results grows as tables get bigger.
We want to understand how the cost of combining tables changes with their size.
Analyze the time complexity of the following SQL query.
SELECT *
FROM TableA
CROSS JOIN TableB;
This query returns every possible pair of rows from TableA and TableB, called the Cartesian product.
Look at what repeats when the query runs.
- Primary operation: For each row in TableA, the database pairs it with every row in TableB.
- How many times: If TableA has n rows and TableB has m rows, this pairing happens n × m times.
As the number of rows grows, the total pairs grow much faster.
| Input Size (n rows in TableA, m rows in TableB) | Approx. Operations (pairs) |
|---|---|
| 10, 10 | 100 |
| 100, 100 | 10,000 |
| 1000, 1000 | 1,000,000 |
Pattern observation: Doubling the size of both tables causes the total pairs to grow by four times, showing a multiplication effect.
Time Complexity: O(n × m)
This means the time to get results grows proportionally to the product of the number of rows in both tables.
[X] Wrong: "The time grows only with the size of one table because we just scan one table at a time."
[OK] Correct: Because the query pairs every row from one table with every row from the other, both table sizes multiply the work, not just one.
Understanding how joins and Cartesian products scale helps you explain query performance clearly and shows you can think about database efficiency in real situations.
What if we changed the CROSS JOIN to an INNER JOIN with a condition that matches only some rows? How would the time complexity change?
Practice
Cartesian product between two tables produce?Solution
Step 1: Understand Cartesian product definition
A Cartesian product combines every row of the first table with every row of the second table.Step 2: Identify the result of combining rows
This means the total rows in the result equal the number of rows in the first table multiplied by the number of rows in the second table.Final Answer:
All possible pairs of rows from both tables -> Option BQuick Check:
Cartesian product = all pairs [OK]
- Confusing Cartesian product with INNER JOIN
- Thinking it returns only matching rows
- Assuming it returns rows from only one table
Solution
Step 1: Identify join types
INNER JOIN combines rows from two tables where a specified condition matches.Step 2: Match keyword to function
CROSS JOIN produces Cartesian product, UNION combines result sets vertically, GROUP BY groups rows. Only INNER JOIN matches rows based on condition.Final Answer:
INNER JOIN -> Option DQuick Check:
Matching rows use INNER JOIN [OK]
- Choosing CROSS JOIN for matching rows
- Confusing UNION with JOIN
- Using GROUP BY to combine tables
Table A: IDs 1, 2Table B: IDs 3, 4What is the number of rows returned by
SELECT * FROM A CROSS JOIN B;?Solution
Step 1: Count rows in each table
Table A has 2 rows, Table B has 2 rows.Step 2: Calculate Cartesian product size
CROSS JOIN returns all pairs: 2 rows * 2 rows = 4 rows.Final Answer:
4 -> Option AQuick Check:
2 * 2 = 4 rows [OK]
- Counting only rows from one table
- Confusing CROSS JOIN with INNER JOIN
- Assuming zero rows if no matching IDs
SELECT * FROM Employees INNER JOIN Departments;Solution
Step 1: Review INNER JOIN syntax
INNER JOIN requires an ON clause to specify how tables relate.Step 2: Check query for ON clause
The query lacks ON condition, so SQL cannot match rows properly.Final Answer:
Missing ON clause for join condition -> Option CQuick Check:
INNER JOIN needs ON clause [OK]
- Omitting ON clause in JOIN
- Thinking INNER JOIN works without condition
- Assuming SELECT * is incorrect
Students (id, name)Grades (student_id, score)Which join will return all students and their scores, showing NULL if a student has no grade?
Solution
Step 1: Understand join types and requirements
LEFT JOIN returns all rows from the left table (Students) and matching rows from the right (Grades). If no match, NULL appears.Step 2: Match requirement to join type
We want all students even if they have no grades, so LEFT JOIN is correct.Final Answer:
LEFT JOIN -> Option AQuick Check:
All left table rows with NULL for no match = LEFT JOIN [OK]
- Using INNER JOIN which excludes unmatched rows
- Choosing RIGHT JOIN which keeps right table rows
- Confusing CROSS JOIN with conditional joins
Ready to prove it?
Pass the test and this pattern moves unknown
