Bird
Raised Fist0
SQLquery~20 mins

LEFT JOIN with NULL result rows in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does a LEFT JOIN do in SQL?
easy
A. Returns all rows from the left table and matched rows from the right table, NULL if no match.
B. Returns only rows that have matching values in both tables.
C. Returns all rows from the right table and matched rows from the left table.
D. Deletes rows from the left table that have no match in the right table.

Solution

  1. Step 1: Understand LEFT JOIN behavior

    A LEFT JOIN keeps all rows from the left table regardless of matches in the right table.
  2. Step 2: Identify NULLs for unmatched rows

    If there is no matching row in the right table, the result shows NULL for right table columns.
  3. Final Answer:

    Returns all rows from the left table and matched rows from the right table, NULL if no match. -> Option A
  4. Quick Check:

    LEFT JOIN = all left rows + NULL for no match [OK]
Hint: LEFT JOIN keeps all left rows, unmatched right rows show NULL [OK]
Common Mistakes:
  • Confusing LEFT JOIN with INNER JOIN
  • Thinking unmatched rows are dropped
  • Assuming NULLs appear in left table columns
2. Which of the following is the correct syntax for a LEFT JOIN in SQL?
easy
A. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id;
B. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 LEFT JOIN table2 USING (id);
D. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

Solution

  1. Step 1: Review standard LEFT JOIN syntax

    The correct syntax is: SELECT columns FROM left_table LEFT JOIN right_table ON condition.
  2. Step 2: Check each option

    SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; matches the correct syntax exactly. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has JOIN LEFT which is invalid. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; is invalid because JOIN requires an ON clause (syntax error). SELECT * FROM table1 LEFT JOIN table2 USING (id); is correct syntax with parentheses.
  3. Final Answer:

    SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option D
  4. Quick Check:

    LEFT JOIN syntax = LEFT JOIN ... ON ... [OK]
Hint: Use LEFT JOIN ... ON ... for correct syntax [OK]
Common Mistakes:
  • Swapping JOIN and LEFT keywords
  • Using WHERE instead of ON for join condition
  • Omitting parentheses in USING clause
3. Given tables Employees and Departments with data:

Employees:
id | name | dept_id
1 | Alice | 10
2 | Bob | 20
3 | Carol | NULL

Departments:
dept_id | dept_name
10 | Sales
20 | HR
30 | IT

What is the result of this query?
SELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.dept_id;
medium
A. [{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}]
B. [{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": null}]
C. [{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": "IT"}]
D. [{"name": "Alice", "dept_name": null}, {"name": "Bob", "dept_name": null}, {"name": "Carol", "dept_name": null}]

Solution

  1. Step 1: Match Employees with Departments by dept_id

    Alice's dept_id 10 matches Sales, Bob's 20 matches HR, Carol's NULL has no match.
  2. Step 2: Apply LEFT JOIN behavior

    All employees appear. For Carol, no matching department, so dept_name is NULL.
  3. Final Answer:

    [{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": null}] -> Option B
  4. Quick Check:

    LEFT JOIN keeps all left rows, unmatched right columns NULL [OK]
Hint: LEFT JOIN shows NULL for unmatched right table rows [OK]
Common Mistakes:
  • Omitting rows with NULL join keys
  • Assuming unmatched rows get default values
  • Confusing INNER JOIN output with LEFT JOIN
4. Consider this SQL query:
SELECT a.id, b.value FROM A a LEFT JOIN B b ON a.id = b.a_id WHERE b.value > 10;

Why might this query return fewer rows than table A has?
medium
A. Because the query syntax is invalid and causes an error.
B. Because LEFT JOIN only returns rows with matching b.value > 10.
C. Because the WHERE clause filters out rows where b.value is NULL, removing unmatched rows.
D. Because the ON condition is incorrect and causes no matches.

Solution

  1. Step 1: Understand LEFT JOIN with WHERE filter

    LEFT JOIN keeps all rows from A, but WHERE filters after join.
  2. Step 2: Effect of WHERE on NULLs from unmatched rows

    Rows with no match have b.value as NULL, and WHERE b.value > 10 excludes NULLs, removing those rows.
  3. Final Answer:

    Because the WHERE clause filters out rows where b.value is NULL, removing unmatched rows. -> Option C
  4. Quick Check:

    WHERE filters NULLs after LEFT JOIN, reducing rows [OK]
Hint: WHERE on right table column after LEFT JOIN filters out NULLs [OK]
Common Mistakes:
  • Thinking LEFT JOIN always keeps all left rows regardless of WHERE
  • Confusing ON and WHERE filtering effects
  • Assuming query syntax error causes fewer rows
5. You have two tables:

Orders:
order_id | customer_id
1 | 101
2 | 102
3 | 103

Customers:
customer_id | name
101 | John
102 | Jane

You want to list all orders with customer names, but show 'Unknown' if no customer found.

Which SQL query correctly achieves this?
hard
A. SELECT o.order_id, COALESCE(c.name, 'Unknown') AS customer_name FROM Orders o LEFT JOIN Customers c ON o.customer_id = c.customer_id;
B. SELECT o.order_id, IFNULL(c.name, 'Unknown') AS customer_name FROM Orders o INNER JOIN Customers c ON o.customer_id = c.customer_id;
C. SELECT o.order_id, c.name FROM Orders o RIGHT JOIN Customers c ON o.customer_id = c.customer_id;
D. SELECT o.order_id, CASE WHEN c.name IS NULL THEN 'Unknown' ELSE c.name END FROM Orders o JOIN Customers c ON o.customer_id = c.customer_id;

Solution

  1. Step 1: Use LEFT JOIN to keep all orders

    LEFT JOIN keeps all orders even if no matching customer exists.
  2. Step 2: Replace NULL customer names with 'Unknown'

    Use COALESCE to show 'Unknown' when c.name is NULL.
  3. Final Answer:

    SELECT o.order_id, COALESCE(c.name, 'Unknown') AS customer_name FROM Orders o LEFT JOIN Customers c ON o.customer_id = c.customer_id; -> Option A
  4. Quick Check:

    LEFT JOIN + COALESCE handles missing customers [OK]
Hint: Use LEFT JOIN with COALESCE to replace NULLs [OK]
Common Mistakes:
  • Using INNER JOIN excludes orders without customers
  • Using RIGHT JOIN reverses table roles incorrectly
  • Forgetting to handle NULL customer names