FULL OUTER JOIN behavior in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using FULL OUTER JOIN, we combine rows from two tables, including all matches and unmatched rows from both sides.
We want to understand how the time needed grows as the tables get bigger.
Analyze the time complexity of the following SQL query.
SELECT A.id, B.value
FROM TableA A
FULL OUTER JOIN TableB B
ON A.id = B.id;
This query returns all rows from both tables, matching rows where IDs are equal, and including unmatched rows with NULLs.
Look for repeated work done by the database engine.
- Primary operation: Comparing rows from TableA and TableB to find matches.
- How many times: Each row in TableA is compared against rows in TableB to find matching IDs.
As the number of rows in both tables grows, the comparisons increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The work grows quickly, roughly multiplying as the product of the two table sizes.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the number of rows in the first table by the number in the second.
[X] Wrong: "FULL OUTER JOIN only looks at one table at a time, so it grows linearly."
[OK] Correct: The join must compare rows from both tables to find matches, so it depends on both sizes together, not just one.
Understanding how joins scale helps you explain query performance clearly and shows you know how databases handle data combinations.
What if we added an index on the join columns? How would the time complexity change?
Practice
FULL OUTER JOIN do in SQL?Solution
Step 1: Understand join types
INNER JOIN returns only matching rows; LEFT JOIN returns all from left plus matches; RIGHT JOIN returns all from right plus matches.Step 2: Define FULL OUTER JOIN behavior
FULL OUTER JOIN returns all rows from both tables, matching where possible, and fills NULLs where no match exists.Final Answer:
Returns all rows from both tables, matching where possible and NULLs where no match exists. -> Option CQuick Check:
FULL OUTER JOIN = all rows both tables [OK]
- Confusing FULL OUTER JOIN with INNER JOIN
- Thinking FULL OUTER JOIN returns only matches
- Mixing up LEFT and RIGHT JOIN behavior
Employees and Departments on DeptID?Solution
Step 1: Identify FULL OUTER JOIN syntax
The correct syntax uses the keywords FULL OUTER JOIN between the two tables with an ON condition.Step 2: Compare options
SELECT * FROM Employees FULL OUTER JOIN Departments ON Employees.DeptID = Departments.DeptID; uses FULL OUTER JOIN correctly; others use INNER, LEFT, or RIGHT JOIN which are different join types.Final Answer:
SELECT * FROM Employees FULL OUTER JOIN Departments ON Employees.DeptID = Departments.DeptID; -> Option AQuick Check:
FULL OUTER JOIN syntax = SELECT * FROM Employees FULL OUTER JOIN Departments ON Employees.DeptID = Departments.DeptID; [OK]
- Using INNER JOIN instead of FULL OUTER JOIN
- Omitting FULL keyword and writing only OUTER JOIN
- Confusing LEFT or RIGHT JOIN with FULL OUTER JOIN
Table A:ID | Name
1 | Alice
2 | Bob
4 | Dana
Table B:ID | City
2 | Boston
3 | Chicago
4 | Denver
What is the result of this query?
SELECT A.ID, A.Name, B.City FROM A FULL OUTER JOIN B ON A.ID = B.ID ORDER BY A.ID;
Solution
Step 1: Match rows by ID using FULL OUTER JOIN
IDs 2 and 4 appear in both tables, so their rows combine. ID 1 is only in A, ID 3 only in B.Step 2: Fill NULLs for missing matches
For ID 1 (A only), City is NULL; for ID 3 (B only), A.ID=NULL, A.Name=NULL, City='Chicago'. ORDER BY A.ID ASC places NULL last: rows for 1,2,4 then NULL row.Final Answer:
[ (1, 'Alice', NULL), (2, 'Bob', 'Boston'), (4, 'Dana', 'Denver'), (NULL, NULL, 'Chicago') ] -> Option DQuick Check:
FULL OUTER JOIN returns all rows with NULLs for missing matches [OK]
- Ignoring unmatched rows from either table
- Assuming INNER JOIN behavior returns only matches
- Misordering results by A.ID when NULLs exist
SELECT * FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID WHERE Orders.OrderID IS NULL;
What is the likely purpose of this query?
Solution
Step 1: Understand FULL OUTER JOIN with WHERE filter
The join returns all customers and orders; filtering WHERE Orders.OrderID IS NULL keeps rows where no matching order exists.Step 2: Interpret the filter effect
Rows with NULL in Orders.OrderID mean customers without orders are selected.Final Answer:
To find customers who have no orders. -> Option BQuick Check:
Filter NULL in Orders = customers without orders [OK]
- Thinking it finds orders without customers
- Assuming it returns all rows without filtering
- Confusing NULL filter with matching rows
Products:ProductID | Name
1 | Pen
2 | Pencil
3 | Eraser
Sales:ProductID | Quantity
2 | 100
3 | 50
4 | 10
You want a query to list all products and sales quantities, including products with no sales and sales for unknown products.
Which query correctly achieves this?
Solution
Step 1: Identify requirement for all products and all sales
We want all products even if no sales, and all sales even if product unknown.Step 2: Choose join type
FULL OUTER JOIN returns all rows from both tables, matching where possible, filling NULLs otherwise.Step 3: Check options
SELECT Products.ProductID, Products.Name, Sales.Quantity FROM Products FULL OUTER JOIN Sales ON Products.ProductID = Sales.ProductID; uses FULL OUTER JOIN correctly; others exclude unmatched rows from one side.Final Answer:
SELECT Products.ProductID, Products.Name, Sales.Quantity FROM Products FULL OUTER JOIN Sales ON Products.ProductID = Sales.ProductID; -> Option AQuick Check:
FULL OUTER JOIN = all products and sales [OK]
- Using LEFT JOIN excludes sales without products
- Using INNER JOIN excludes unmatched rows
- Confusing RIGHT JOIN direction with LEFT JOIN
