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;
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');
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.
Rows from Employees are all included. For DeptID 10 and 20, matching DeptName is shown. For DeptID NULL or 30 (no match), DeptName is NULL.
Why do some columns from the right table show NULL in a LEFT JOIN result?
Think about what LEFT JOIN means: all rows from the left table, matched rows from the right table or NULL if no match.
When a row in the left table has no matching row in the right table, the columns from the right table appear as NULL in the result.
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;
Check the syntax of the WHERE clause and NULL comparisons.
Option B has incorrect syntax: WHERE IS NULL d.DeptName is invalid SQL syntax.
Option B uses = NULL which is syntactically valid but logically incorrect (it returns no rows), not a syntax error.
Option B uses HAVING which is valid syntax but not appropriate here.
You want to find all employees without a department assigned. Which query is more efficient?
Think about how to efficiently find rows in one table that have no matching rows in another.
Option C uses NOT EXISTS which is often more efficient than LEFT JOIN with NULL filtering.
Option C uses LEFT JOIN and filters NULLs but can be slower on large data.
Option C only finds employees with NULL DeptID, missing those with invalid DeptID values.
Option C is invalid because INNER JOIN never returns NULLs.
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?
Check data types and values used in the JOIN condition.
If DeptID values do not match exactly (e.g., one is string, other is int, or trailing spaces), the JOIN fails and rows appear with NULL DeptName.
This causes employees with valid departments to appear as unmatched.