FULL OUTER JOIN in PostgreSQL - Time & Space Complexity
When we use a FULL OUTER JOIN in a database, we combine rows from two tables, keeping all rows from both sides.
We want to understand how the time it takes grows as the tables get bigger.
Analyze the time complexity of the following code snippet.
SELECT *
FROM table_a
FULL OUTER JOIN table_b
ON table_a.id = table_b.id;
This query returns all rows from both tables, matching rows where the id is the same, and filling with NULLs where there is no match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing rows from both tables to find matches.
- How many times: Each row in the first table is compared to rows in the second table to find matching ids.
As the number of rows in each table grows, the work to find matching rows grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The number of comparisons grows quickly as both tables get bigger, roughly multiplying their sizes.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the number of rows in the first table (n) by the number of rows in the second table (m).
[X] Wrong: "FULL OUTER JOIN only looks at one table at a time, so time grows linearly."
[OK] Correct: The join must compare rows from both tables to find matches, so it depends on both table sizes together, not just one.
Understanding how joins scale helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we added an index on the join column? How would the time complexity change?"
Practice
FULL OUTER JOIN do in PostgreSQL?Solution
Step 1: Understand the join types
INNER JOIN returns only matching rows; LEFT JOIN returns all left table rows plus matches; RIGHT JOIN returns all right table rows plus matches.Step 2: Define FULL OUTER JOIN behavior
FULL OUTER JOIN returns all rows from both tables, matching where possible, and fills unmatched columns with NULL.Final Answer:
It returns all rows from both tables, matching where possible, and NULLs where no match exists. -> Option DQuick Check:
FULL OUTER JOIN = all rows both tables [OK]
- Confusing FULL OUTER JOIN with INNER JOIN
- Thinking FULL OUTER JOIN returns only left or right table rows
- Assuming unmatched rows are excluded
Solution
Step 1: Recall PostgreSQL FULL OUTER JOIN syntax
The correct syntax is: SELECT ... FROM table1 FULL OUTER JOIN table2 ON condition;Step 2: Check each option
The syntax 'SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id;' matches exactly. Others err: 'JOIN FULL OUTER' wrong order; 'FULL JOIN ... WHERE' misuses WHERE for join; 'OUTER FULL JOIN' wrong sequence.Final Answer:
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id; -> Option AQuick Check:
FULL OUTER JOIN syntax = FULL OUTER JOIN ON [OK]
- Swapping JOIN and FULL OUTER keywords
- Omitting ON clause
- Using WHERE instead of ON for join condition
Table A:ID | Name
1 | Alice
2 | Bob
4 | Diana
Table B:ID | City
2 | Paris
3 | London
4 | Berlin
What is the result of:
SELECT COALESCE(A.ID, B.ID) AS ID, A.Name, B.City FROM A FULL OUTER JOIN B ON A.ID = B.ID ORDER BY ID;Solution
Step 1: Identify matching and unmatched IDs
Matching IDs: 2 and 4. Unmatched in A: 1. Unmatched in B: 3.Step 2: Construct FULL OUTER JOIN result
Rows:
- ID 1: Name 'Alice', City NULL (no match in B)
- ID 2: Name 'Bob', City 'Paris' (match)
- ID 3: Name NULL, City 'London' (no match in A)
- ID 4: Name 'Diana', City 'Berlin' (match)Final Answer:
[{1, 'Alice', NULL}, {2, 'Bob', 'Paris'}, {3, NULL, 'London'}, {4, 'Diana', 'Berlin'}] -> Option CQuick Check:
FULL OUTER JOIN includes all rows with NULLs for unmatched [OK]
- Excluding unmatched rows from either table
- Mixing up NULL placement
- Ordering rows incorrectly
SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id WHERE A.name = 'Alice';Why might this query return fewer rows than expected?
Solution
Step 1: Understand FULL OUTER JOIN with WHERE clause
FULL OUTER JOIN returns all rows, but WHERE filters after join. Filtering on A.name excludes rows where A.name is NULL (unmatched in A).Step 2: Effect of filtering on unmatched rows
Rows unmatched in A have NULL for A.name, so WHERE A.name = 'Alice' removes them, reducing rows.Final Answer:
Because the WHERE clause filters out rows where A.name is NULL, excluding unmatched rows from B. -> Option AQuick Check:
WHERE filters after join, can exclude NULLs [OK]
- Thinking FULL OUTER JOIN ignores WHERE filters
- Assuming ON clause filters rows after join
- Believing GROUP BY affects join row count
Employees(emp_id, name) and Departments(dept_id, emp_id, dept_name).You want a list of all employees and their departments if any, plus all departments and their employees if any.
Which query correctly uses FULL OUTER JOIN to achieve this?
Solution
Step 1: Understand the requirement
We want all employees and their departments if any, plus all departments and their employees if any. This means all rows from both tables.Step 2: Choose the correct join type
FULL OUTER JOIN returns all rows from both tables, matching where possible. LEFT or RIGHT JOIN only return all rows from one table.Final Answer:
SELECT e.emp_id, e.name, d.dept_name FROM Employees e FULL OUTER JOIN Departments d ON e.emp_id = d.emp_id; -> Option BQuick Check:
FULL OUTER JOIN returns all rows both tables [OK]
- Using LEFT or RIGHT JOIN excludes unmatched rows from one table
- Using INNER JOIN excludes unmatched rows
- Forgetting ON condition
Ready to prove it?
Pass the test and this pattern moves unknown
