0
0
SQLquery~20 mins

LEFT JOIN with NULL result rows 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 unmatched rows

Consider two tables: Employees and Departments.

Employees has columns EmpID, Name, and DeptID. Departments has columns DeptID and DeptName.

What is the output of this query?

SELECT e.EmpID, e.Name, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID
ORDER BY e.EmpID;
SQL
CREATE TABLE Employees (EmpID INT, Name VARCHAR(20), DeptID INT);
CREATE TABLE Departments (DeptID INT, DeptName VARCHAR(20));

INSERT INTO Employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL), (4, 'Diana', 30);
INSERT INTO Departments VALUES (10, 'HR'), (20, 'Finance');
A
1, Alice, HR
2, Bob, Finance
3, Charlie, NULL
4, Diana, NULL
B
1, Alice, HR
2, Bob, Finance
3, Charlie, NULL
C
1, Alice, HR
2, Bob, Finance
4, Diana, NULL
D
1, Alice, HR
2, Bob, Finance
3, Charlie, NULL
4, Diana, 30
Attempts:
2 left
💡 Hint

LEFT JOIN returns all rows from the left table, even if there is no matching row in the right table. If no match, columns from the right table are NULL.

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

Why do some columns from the right table show NULL in a LEFT JOIN result?

ABecause there is no matching row in the right table for the left table row
BBecause the left table columns are NULL and cause right table columns to be NULL
CBecause the right table has NULL values in those columns for matching rows
DBecause the LEFT JOIN filters out rows with non-NULL values in the right table
Attempts:
2 left
💡 Hint

Think about what LEFT JOIN means: all rows from the left table, matched rows from the right table or NULL if no match.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in LEFT JOIN query

Which option contains a syntax error in this LEFT JOIN query?

SELECT e.EmpID, e.Name, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID
WHERE d.DeptName IS NULL;
ASELECT e.EmpID, e.Name, d.DeptName FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID HAVING d.DeptName IS NULL;
BSELECT e.EmpID, e.Name, d.DeptName FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID WHERE IS NULL d.DeptName;
CSELECT e.EmpID, e.Name, d.DeptName FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID WHERE d.DeptName IS NULL;
DSELECT e.EmpID, e.Name, d.DeptName FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID WHERE d.DeptName = NULL;
Attempts:
2 left
💡 Hint

Check the syntax of the WHERE clause and NULL comparisons.

optimization
advanced
2:30remaining
Optimizing LEFT JOIN with NULL filtering

You want to find all employees without a department assigned. Which query is more efficient?

ASELECT e.EmpID, e.Name FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID WHERE d.DeptID IS NULL;
BSELECT e.EmpID, e.Name FROM Employees e WHERE e.DeptID IS NULL;
CSELECT e.EmpID, e.Name FROM Employees e WHERE NOT EXISTS (SELECT 1 FROM Departments d WHERE d.DeptID = e.DeptID);
DSELECT e.EmpID, e.Name FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID WHERE d.DeptID IS NULL;
Attempts:
2 left
💡 Hint

Think about how to efficiently find rows in one table that have no matching rows in another.

🔧 Debug
expert
3:00remaining
Debugging unexpected NULLs in LEFT JOIN result

You run this query:

SELECT e.EmpID, e.Name, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID
WHERE d.DeptName IS NULL;

But you notice some employees with valid departments are missing. What is the most likely cause?

AThe LEFT JOIN is filtering out employees with NULL <code>DeptID</code>
BThe query should use INNER JOIN instead of LEFT JOIN
CThe WHERE clause should be <code>WHERE e.DeptID IS NULL</code> instead
DSome <code>DeptID</code> values in <code>Employees</code> do not exactly match <code>DeptID</code> in <code>Departments</code> due to data type mismatch or trailing spaces
Attempts:
2 left
💡 Hint

Check data types and values used in the JOIN condition.