Challenge - 5 Problems
NATURAL JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of NATURAL JOIN with overlapping columns
Given two tables Employees and Departments with the following data:
Employees
id | name | dept_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
Departments
dept_id | dept_name
10 | Sales
20 | Marketing
What is the result of the query:
Employees
id | name | dept_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | 10
Departments
dept_id | dept_name
10 | Sales
20 | Marketing
What is the result of the query:
SELECT * FROM Employees NATURAL JOIN Departments;
PostgreSQL
SELECT * FROM Employees NATURAL JOIN Departments;
Attempts:
2 left
💡 Hint
NATURAL JOIN matches rows based on all columns with the same name in both tables.
✗ Incorrect
NATURAL JOIN automatically joins on columns with the same name, here 'dept_id'. All employees with matching dept_id get joined with their department name.
🧠 Conceptual
intermediate1:30remaining
Risk of unintended column matches in NATURAL JOIN
Why can using NATURAL JOIN be risky in queries involving multiple tables?
Attempts:
2 left
💡 Hint
Think about what happens if tables share column names that are not meant to be join keys.
✗ Incorrect
NATURAL JOIN automatically joins on all columns with the same name. If tables share columns that are not intended as keys, the join may produce wrong or unexpected results.
📝 Syntax
advanced1:30remaining
Identify the error in NATURAL JOIN usage
Which of the following queries will cause a syntax error in PostgreSQL?
Attempts:
2 left
💡 Hint
Check if the NATURAL JOIN syntax is complete and correct.
✗ Incorrect
Option C is missing the table name after NATURAL JOIN, causing a syntax error. NATURAL JOIN does not accept an ON clause, so option B is invalid syntax as well.
❓ query_result
advanced2:00remaining
Effect of adding a new column with same name on NATURAL JOIN
Consider two tables:
Orders
order_id | customer_id | status
1 | 101 | shipped
2 | 102 | pending
Shipments
shipment_id | order_id | status
501 | 1 | delivered
502 | 2 | in transit
What is the result of:
Orders
order_id | customer_id | status
1 | 101 | shipped
2 | 102 | pending
Shipments
shipment_id | order_id | status
501 | 1 | delivered
502 | 2 | in transit
What is the result of:
SELECT * FROM Orders NATURAL JOIN Shipments;
PostgreSQL
SELECT * FROM Orders NATURAL JOIN Shipments;
Attempts:
2 left
💡 Hint
NATURAL JOIN joins on all columns with the same name, including 'status'.
✗ Incorrect
Since both tables have 'order_id' and 'status' columns, NATURAL JOIN joins on both. Only rows where both 'order_id' and 'status' match appear. Here, no rows match on both columns, so the join returns no rows.
🔧 Debug
expert2:30remaining
Diagnose unexpected results from NATURAL JOIN
A developer runs this query:
Both tables have columns: product_id, name, and quantity.
They expect to get all products with their inventory quantities, but the result is empty.
What is the most likely cause?
SELECT * FROM Products NATURAL JOIN Inventory;
Both tables have columns: product_id, name, and quantity.
They expect to get all products with their inventory quantities, but the result is empty.
What is the most likely cause?
Attempts:
2 left
💡 Hint
Think about how NATURAL JOIN decides which columns to join on.
✗ Incorrect
NATURAL JOIN uses all columns with the same name as join keys. If 'name' and 'quantity' differ between tables, no rows match on all columns, resulting in empty output.