Inner join in Data Analysis Python - Time & Space Complexity
When we combine two tables using an inner join, we want to know how the time to do this grows as the tables get bigger.
We ask: How does the work increase when the number of rows in each table grows?
Analyze the time complexity of the following code snippet.
import pandas as pd
df1 = pd.DataFrame({'key': [1, 2, 3], 'value1': ['A', 'B', 'C']})
df2 = pd.DataFrame({'key': [2, 3, 4], 'value2': ['X', 'Y', 'Z']})
result = pd.merge(df1, df2, on='key', how='inner')
print(result)
This code joins two tables on a common column called 'key' and keeps only matching rows.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row in the first table against rows in the second table to find matching keys.
- How many times: For each row in the first table, the algorithm looks for matching rows in the second table.
As the number of rows in both tables grows, the number of comparisons grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The work grows much faster than the input size because each row in one table is compared to many rows in the other.
Time Complexity: O(n * m)
This means the time to join grows roughly by multiplying the number of rows in the first table by the number of rows in the second table.
[X] Wrong: "The inner join time grows only with the size of one table."
[OK] Correct: Because the join must compare rows from both tables, the time depends on both sizes, not just one.
Understanding how joins scale helps you explain database performance clearly and shows you know what happens behind the scenes.
"What if one table has an index on the join column? How would the time complexity change?"
Practice
inner join do when combining two tables?Solution
Step 1: Understand join types
Inner join returns rows where keys match in both tables only.Step 2: Compare options with definition
The option 'Returns only rows with matching keys in both tables' describes this behavior correctly.Final Answer:
Returns only rows with matching keys in both tables -> Option AQuick Check:
Inner join = matching rows only [OK]
- Confusing inner join with left or right join
- Thinking inner join returns unmatched rows
- Mixing up join types and their results
df1 and df2 on column 'id'?Solution
Step 1: Identify pandas inner join syntax
The correct function ispd.merge()withhow='inner'andon='id'.Step 2: Check each option
pd.merge(df1, df2, on='id', how='inner') matches this exactly; others use wrong methods or join types.Final Answer:
pd.merge(df1, df2, on='id', how='inner') -> Option CQuick Check:
pd.merge + how='inner' = inner join [OK]
- Using join() instead of merge() for inner join
- Using wrong join type like 'outer' or 'left'
- Using concat which does not perform join
df1 = pd.DataFrame({'id': [1, 2, 3], 'val1': ['A', 'B', 'C']})
df2 = pd.DataFrame({'id': [2, 3, 4], 'val2': ['X', 'Y', 'Z']})
result = pd.merge(df1, df2, on='id', how='inner')What is the content of
result?Solution
Step 1: Identify matching keys for inner join
Keys in both df1 and df2 are 2 and 3 only.Step 2: Merge rows with matching keys
Rows with id 2 and 3 combine val1 and val2 columns.Final Answer:
{'id': [2, 3], 'val1': ['B', 'C'], 'val2': ['X', 'Y']} -> Option AQuick Check:
Inner join keeps ids 2 and 3 only [OK]
- Including keys not present in both tables
- Confusing inner join with outer join
- Assuming all rows are kept
result = pd.merge(df1, df2, how='inner')
But it raises an error. What is the likely cause?
Solution
Step 1: Check required parameters for pd.merge
pd.merge requires 'on' or 'left_on'/'right_on' to specify join keys.Step 2: Identify missing 'on' parameter
Code misses 'on' parameter, causing error.Final Answer:
Missing the 'on' parameter to specify join key -> Option BQuick Check:
pd.merge needs 'on' key [OK]
- Forgetting to specify join keys
- Assuming default join key exists
- Trying to join incompatible data types
df1 = pd.DataFrame({'id': [1, 2, 2, 3], 'val1': ['A', 'B', 'C', 'D']})
df2 = pd.DataFrame({'id': [2, 2, 3], 'val2': ['X', 'Y', 'Z']})
result = pd.merge(df1, df2, on='id', how='inner')How many rows will
result contain after the inner join?Solution
Step 1: Identify matching keys and duplicates
Keys 2 and 3 appear multiple times in both DataFrames.Step 2: Calculate Cartesian product for duplicates
For id=2: df1 has 2 rows, df2 has 2 rows -> 2*2=4 rows.
For id=3: df1 has 1 row, df2 has 1 row -> 1*1=1 row.
Total rows = 4 + 1 = 5 rows.Step 3: Recount carefully
Recount confirms 5 rows total.Final Answer:
5 -> Option DQuick Check:
Duplicates multiply rows in inner join [OK]
- Ignoring duplicate keys causing more rows
- Counting unique keys only
- Assuming one-to-one join always
Ready to prove it?
Pass the test and this pattern moves unknown
