Consider two tables:
Employees
id | name
1 | Alice
2 | Bob
3 | Carol
Departments
id | dept_name
2 | Sales
3 | HR
4 | IT
What is the result of this query?
SELECT Employees.id, Employees.name, Departments.dept_name FROM Employees FULL OUTER JOIN Departments ON Employees.id = Departments.id ORDER BY Employees.id NULLS LAST, Departments.id NULLS LAST;
SELECT Employees.id, Employees.name, Departments.dept_name FROM Employees FULL OUTER JOIN Departments ON Employees.id = Departments.id ORDER BY Employees.id NULLS LAST, Departments.id NULLS LAST;
FULL OUTER JOIN returns all rows from both tables, matching where possible, and NULL where no match exists.
The FULL OUTER JOIN returns all rows from both Employees and Departments. For id=1, only Employees has a row, so Departments columns are NULL. For id=4, only Departments has a row, so Employees columns are NULL. For ids 2 and 3, rows match and show combined data.
When using FULL OUTER JOIN between two tables, which statement about NULL values in the result is true?
Think about what happens when a row exists in one table but not the other.
FULL OUTER JOIN returns all rows from both tables. When a row from one table has no matching row in the other, the columns from the other table are filled with NULLs.
Which of the following SQL queries will cause a syntax error?
SELECT A.id, B.name FROM TableA A FULL OUTER JOIN TableB B ON A.id = B.id
Check the order of keywords in the JOIN clause.
The correct syntax is FULL OUTER JOIN or FULL JOIN (depending on the database). 'OUTER FULL JOIN' is invalid syntax.
You have two large tables with millions of rows each. You want to perform a FULL OUTER JOIN on a key column. Which approach can improve query performance?
Indexes help the database find matching rows faster.
Creating indexes on join keys helps the database quickly locate matching rows, improving FULL OUTER JOIN performance. CROSS JOIN generates all combinations and is very slow. Removing indexes slows down joins. Using subqueries with LEFT JOIN may not cover all rows from both tables.
You run this query:
SELECT A.id, A.value, B.value FROM TableA A FULL OUTER JOIN TableB B ON A.id = B.id WHERE A.value > 10;
You notice that rows with NULL in A.value do not appear in the result. Why?
Think about when the WHERE clause is applied in relation to the JOIN.
The WHERE clause is applied after the FULL OUTER JOIN. Rows from TableB without matching TableA rows have NULL in A.value. The condition A.value > 10 evaluates to UNKNOWN for NULLs, so those rows are excluded. However, if the condition is in WHERE, rows with NULL A.value are filtered out. If you want to keep all rows, use the condition in the JOIN or use OR A.value IS NULL.