FULL OUTER JOIN availability across databases in SQL - Time & Space Complexity
We want to understand how the time it takes to run a FULL OUTER JOIN changes as the size of the tables grows.
How does the database handle joining all rows from two tables, even when some rows don't match?
Analyze the time complexity of the following SQL query using FULL OUTER JOIN.
SELECT a.id, a.value, b.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 possible, and filling with NULLs where no match exists.
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 to rows in TableB, and vice versa, depending on join method.
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 roughly with the product of the sizes of the two tables.
Time Complexity: O(n * m)
This means the time to run the FULL OUTER JOIN grows roughly with the number of rows in both tables multiplied together.
[X] Wrong: "FULL OUTER JOIN runs as fast as INNER JOIN because it's just joining tables."
[OK] Correct: FULL OUTER JOIN must include all rows from both tables, even unmatched ones, so it often requires more work than INNER JOIN.
Understanding how joins scale helps you explain database performance clearly and shows you know how data size affects query speed.
"What if we replaced FULL OUTER JOIN with LEFT JOIN? How would the time complexity change?"
Practice
FULL OUTER JOIN clause?Solution
Step 1: Understand FULL OUTER JOIN support in popular databases
PostgreSQL supports FULL OUTER JOIN natively, allowing retrieval of all matching and non-matching rows from both tables.Step 2: Check other databases' support
MySQL before 8.0 and SQLite do not support FULL OUTER JOIN natively; MariaDB added support only after version 10.2.Final Answer:
PostgreSQL -> Option AQuick Check:
PostgreSQL supports FULL OUTER JOIN [OK]
- Assuming MySQL supports FULL OUTER JOIN before version 8.0
- Thinking SQLite supports FULL OUTER JOIN
- Confusing MariaDB versions for FULL OUTER JOIN support
Solution
Step 1: Identify correct FULL OUTER JOIN syntax
The correct syntax usesFULL OUTER JOINwith anONclause to specify join condition.Step 2: Analyze incorrect options
SELECT * FROM table1 INNER FULL JOIN table2 ON table1.id = table2.id; uses invalid keywordINNER FULL JOIN, SELECT * FROM table1 FULL JOIN table2 WHERE table1.id = table2.id; misplaces join condition inWHERE, SELECT * FROM table1 OUTER JOIN table2 ON table1.id = table2.id; uses incompleteOUTER JOINwithout FULL.Final Answer:
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id; -> Option AQuick Check:
FULL OUTER JOIN syntax = SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id; [OK]
- Using INNER FULL JOIN instead of FULL OUTER JOIN
- Placing join condition in WHERE instead of ON
- Omitting FULL keyword before OUTER JOIN
Table A: id, nameTable B: id, cityWith data:
Table A: (1, 'Alice'), (2, 'Bob')
Table B: (2, 'Paris'), (3, 'London')
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, B.id;
Solution
Step 1: Understand FULL OUTER JOIN behavior
It returns all rows from both tables, matching rows where possible, NULL where no match.Step 2: Match rows by id and fill NULLs
id=1 only in A, so city NULL; id=2 in both, so matched; id=3 only in B, so name NULL.Final Answer:
(1, 'Alice', NULL), (2, 'Bob', 'Paris'), (3, NULL, 'London') -> Option DQuick Check:
FULL OUTER JOIN returns all rows with NULLs for missing matches [OK]
- Expecting only matched rows like INNER JOIN
- Confusing NULL placement for unmatched rows
- Ignoring rows unique to one table
SELECT * FROM employees FULL OUTER JOIN departments ON employees.dept_id = departments.id;
It returns a syntax error. What is the best fix?
Solution
Step 1: Identify MySQL 5.7 limitation
MySQL 5.7 does not support FULL OUTER JOIN syntax, causing syntax error.Step 2: Use workaround with LEFT JOIN and RIGHT JOIN UNION
Combine LEFT JOIN and RIGHT JOIN with UNION to simulate FULL OUTER JOIN in MySQL 5.7.Final Answer:
Replace FULL OUTER JOIN with LEFT JOIN and UNION with RIGHT JOIN -> Option BQuick Check:
MySQL 5.7 needs LEFT JOIN + RIGHT JOIN UNION for FULL OUTER JOIN [OK]
- Trying to use FULL OUTER JOIN directly in MySQL 5.7
- Using CROSS JOIN which returns Cartesian product
- Thinking parentheses fix syntax error
Solution
Step 1: Recognize SQLite limitation
SQLite does not support FULL OUTER JOIN, so we must simulate it.Step 2: Simulate FULL OUTER JOIN with UNION of LEFT and RIGHT JOIN
Use LEFT JOIN customers to orders plus UNION ALL with RIGHT JOIN orders to customers (emulated by LEFT JOIN orders to customers) filtering duplicates to get all rows.Final Answer:
Use LEFT JOIN customers to orders UNION ALL RIGHT JOIN orders to customers with filtering duplicates -> Option CQuick Check:
Simulate FULL OUTER JOIN with LEFT + RIGHT JOIN UNION [OK]
- Using INNER JOIN which excludes unmatched rows
- Using CROSS JOIN which creates all combinations
- Using only LEFT JOIN missing unmatched rows from right table
Ready to prove it?
Pass the test and this pattern moves unknown
