Challenge - 5 Problems
LEFT JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of LEFT JOIN with no matching rows
Given two tables Employees and Departments, what is the output of the following query?
Assume Employees has 3 rows with DeptID values 1, 2, and NULL, and Departments has IDs 1 and 2 but no department named 'Marketing'.
SELECT Employees.Name, Departments.Name AS DeptName FROM Employees LEFT JOIN Departments ON Employees.DeptID = Departments.ID WHERE Departments.Name = 'Marketing';
Assume Employees has 3 rows with DeptID values 1, 2, and NULL, and Departments has IDs 1 and 2 but no department named 'Marketing'.
SQL
SELECT Employees.Name, Departments.Name AS DeptName FROM Employees LEFT JOIN Departments ON Employees.DeptID = Departments.ID WHERE Departments.Name = 'Marketing';
Attempts:
2 left
💡 Hint
Remember that WHERE filters after the join, so conditions on the right table can exclude rows.
✗ Incorrect
The LEFT JOIN keeps all Employees rows, but the WHERE clause filters rows where Departments.Name is 'Marketing'. Since no department has that name, Departments.Name is NULL for all rows, so the WHERE clause excludes all rows, resulting in no output.
❓ query_result
intermediate2:00remaining
LEFT JOIN with condition in ON clause vs WHERE clause
Consider the tables Orders and Customers. What is the difference in output between these two queries?
Query 1:
Query 2:
Query 1:
SELECT Orders.ID, Customers.Name FROM Orders LEFT JOIN Customers ON Orders.CustomerID = Customers.ID AND Customers.Status = 'Active';
Query 2:
SELECT Orders.ID, Customers.Name FROM Orders LEFT JOIN Customers ON Orders.CustomerID = Customers.ID WHERE Customers.Status = 'Active';
SQL
SELECT Orders.ID, Customers.Name FROM Orders LEFT JOIN Customers ON Orders.CustomerID = Customers.ID AND Customers.Status = 'Active';
Attempts:
2 left
💡 Hint
Think about when the condition is applied: during join or after join.
✗ Incorrect
In Query 1, the condition on Customers.Status is part of the join condition, so unmatched or inactive customers cause NULLs but orders still appear. In Query 2, the WHERE clause filters after the join, removing rows where Customers.Status is not 'Active', so only orders with active customers appear.
📝 Syntax
advanced2:00remaining
Identify the syntax error in LEFT JOIN query
Which of the following LEFT JOIN queries will cause a syntax error?
Attempts:
2 left
💡 Hint
Look carefully at the WHERE clause syntax.
✗ Incorrect
Option D has a WHERE clause with no condition after it, which is a syntax error. The other queries have correct syntax.
❓ optimization
advanced2:00remaining
Optimizing LEFT JOIN with large tables
You have two large tables: Sales (millions of rows) and Products. You want to list all sales with product names, including sales with missing product info. Which approach is more efficient?
Attempts:
2 left
💡 Hint
Filtering in ON clause can reduce rows early in join.
✗ Incorrect
Filtering conditions on the right table should be in the ON clause for LEFT JOIN to avoid turning it into an INNER JOIN and to optimize performance by reducing rows during join.
🧠 Conceptual
expert2:00remaining
Understanding NULL propagation in LEFT JOIN
In a LEFT JOIN between Table1 and Table2, if a row in Table1 has no matching row in Table2, what will be the value of columns from Table2 in the result?
Attempts:
2 left
💡 Hint
Think about what LEFT JOIN means for unmatched rows.
✗ Incorrect
LEFT JOIN returns all rows from the left table and fills columns from the right table with NULL when there is no match.