0
0
PostgreSQLquery~20 mins

FULL OUTER JOIN in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FULL OUTER JOIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of FULL OUTER JOIN with matching and non-matching rows

Consider two tables Employees and Departments:

Employees:
+----+----------+-----------+
| id | name     | dept_id   |
+----+----------+-----------+
| 1  | Alice    | 10        |
| 2  | Bob      | 20        |
| 3  | Charlie  | NULL      |
+----+----------+-----------+

Departments:
+----+------------+
| id | dept_name  |
+----+------------+
| 10 | HR         |
| 30 | Marketing  |
+----+------------+

What is the result of this query?

SELECT e.name, d.dept_name
FROM Employees e
FULL OUTER JOIN Departments d ON e.dept_id = d.id
ORDER BY e.name NULLS LAST, d.dept_name NULLS LAST;
PostgreSQL
SELECT e.name, d.dept_name
FROM Employees e
FULL OUTER JOIN Departments d ON e.dept_id = d.id
ORDER BY e.name NULLS LAST, d.dept_name NULLS LAST;
A
+---------+------------+
| name    | dept_name  |
+---------+------------+
| Alice   | HR         |
| Bob     | NULL       |
| Charlie | NULL       |
| NULL    | Marketing  |
+---------+------------+
B
+---------+------------+
| name    | dept_name  |
+---------+------------+
| Alice   | HR         |
| Bob     | NULL       |
| NULL    | Marketing  |
+---------+------------+
C
+---------+------------+
| name    | dept_name  |
+---------+------------+
| Alice   | HR         |
| Bob     | NULL       |
| Charlie | NULL       |
+---------+------------+
D
+---------+------------+
| name    | dept_name  |
+---------+------------+
| Alice   | HR         |
| NULL    | Marketing  |
+---------+------------+
Attempts:
2 left
💡 Hint

FULL OUTER JOIN returns all rows from both tables. If there is no match, NULL fills the missing side.

🧠 Conceptual
intermediate
1:30remaining
Understanding NULLs in FULL OUTER JOIN results

When using FULL OUTER JOIN between two tables, which statement about NULL values in the result is always true?

ANULL values never appear in the result of a FULL OUTER JOIN.
BNULL values appear only in columns from the table on the left side of the join.
CNULL values appear only in columns from the table on the right side of the join.
DNULL values appear in columns from either table when there is no matching row in the other table.
Attempts:
2 left
💡 Hint

Think about what happens when a row in one table has no matching row in the other.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in FULL OUTER JOIN query

Which of the following FULL OUTER JOIN queries will cause a syntax error in PostgreSQL?

ASELECT * FROM A FULL OUTER JOIN B ON A.id = B.id;
BSELECT * FROM A FULL OUTER JOIN B ON A.id == B.id;
CSELECT * FROM A FULL JOIN B ON A.id = B.id;
DSELECT * FROM A FULL OUTER JOIN B USING (id);
Attempts:
2 left
💡 Hint

Check the operator used for equality in the ON clause.

optimization
advanced
2:00remaining
Optimizing FULL OUTER JOIN with large tables

You have two large tables, Orders and Shipments, joined by FULL OUTER JOIN on order_id. Which approach can improve query performance?

AAdd indexes on order_id columns in both tables before running the FULL OUTER JOIN.
BReplace FULL OUTER JOIN with CROSS JOIN to reduce complexity.
CRemove WHERE clause filters to allow the database to optimize better.
DUse SELECT * to fetch all columns to avoid missing data.
Attempts:
2 left
💡 Hint

Indexes help speed up join operations on large tables.

🔧 Debug
expert
2:30remaining
Diagnose unexpected NULLs in FULL OUTER JOIN result

You run this query:

SELECT e.id, e.name, d.dept_name
FROM Employees e
FULL OUTER JOIN Departments d ON e.dept_id = d.id
WHERE d.dept_name = 'Sales';

You expect to see all employees in Sales department, but the result is empty. Why?

AThe ON condition is incorrect; it should be e.id = d.id.
BFULL OUTER JOIN does not work with WHERE clauses.
CThe WHERE clause filters out rows where d.dept_name is NULL, removing unmatched employees.
DEmployees table has no rows with dept_id matching any department.
Attempts:
2 left
💡 Hint

Think about how WHERE filters affect NULLs from unmatched rows.