0
0
SQLquery~20 mins

LEFT JOIN execution behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LEFT JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of LEFT JOIN with no matching rows
Given two tables Employees and Departments, what is the output of the following query?

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';
AThe query returns an error because Departments.Name is NULL for some rows.
BNo rows are returned because the WHERE clause filters out all rows where Departments.Name is NULL.
COnly employees with DeptID matching a department named 'Marketing' are returned.
DAll employees are returned with DeptName as NULL because LEFT JOIN keeps all Employees rows.
Attempts:
2 left
💡 Hint
Remember that WHERE filters after the join, so conditions on the right table can exclude rows.
query_result
intermediate
2: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:
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';
AQuery 1 returns all orders with NULL for inactive customers; Query 2 returns only orders with active customers.
BBoth queries return the same rows because the condition is applied in both cases.
CQuery 1 returns only orders with active customers; Query 2 returns all orders with NULL for inactive customers.
DQuery 1 returns an error due to condition in ON clause; Query 2 runs correctly.
Attempts:
2 left
💡 Hint
Think about when the condition is applied: during join or after join.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in LEFT JOIN query
Which of the following LEFT JOIN queries will cause a syntax error?
ASELECT a.id, b.value FROM tableA a LEFT JOIN tableB b ON a.id = b.a_id;
BSELECT a.id, b.value FROM tableA a LEFT JOIN tableB b ON a.id = b.a_id WHERE b.value IS NOT NULL;
CSELECT * FROM tableA LEFT JOIN tableB ON tableA.id = tableB.a_id;
DSELECT * FROM tableA LEFT JOIN tableB ON tableA.id = tableB.a_id WHERE;
Attempts:
2 left
💡 Hint
Look carefully at the WHERE clause syntax.
optimization
advanced
2: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?
AUse LEFT JOIN with condition on Products in ON clause to filter products.
BUse INNER JOIN to only get sales with matching products.
CUse LEFT JOIN with condition on Products in WHERE clause to filter products.
DUse CROSS JOIN and filter in WHERE clause.
Attempts:
2 left
💡 Hint
Filtering in ON clause can reduce rows early in join.
🧠 Conceptual
expert
2: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?
AThey will contain zero or empty string depending on data type.
BThey will contain default values defined in Table2 schema.
CThey will contain NULL values.
DThey will contain values from the previous matched row.
Attempts:
2 left
💡 Hint
Think about what LEFT JOIN means for unmatched rows.