Bird
Raised Fist0
SQLquery~20 mins

LEFT JOIN preserving all left 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 preserving all left rows
Given two tables, Employees and Departments, what is the output of the following SQL query?

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID
ORDER BY Employees.ID;

Assume the tables contain:
Employees:
ID | Name | DepartmentID
1 | Alice | 10
2 | Bob | 20
3 | Charlie | NULL

Departments:
ID | DepartmentName
10 | HR
20 | IT
SQL
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID
ORDER BY Employees.ID;
A[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": null}, {"Name": "Charlie", "DepartmentName": null}]
B[{"Name": "Alice", "DepartmentName": null}, {"Name": "Bob", "DepartmentName": null}, {"Name": "Charlie", "DepartmentName": null}]
C[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": "IT"}, {"Name": "Charlie", "DepartmentName": null}]
D[{"Name": "Alice", "DepartmentName": "HR"}, {"Name": "Bob", "DepartmentName": "IT"}]
Attempts:
2 left
💡 Hint
Remember, LEFT JOIN keeps all rows from the left table even if there is no matching row in the right table.
🧠 Conceptual
intermediate
1:30remaining
Understanding LEFT JOIN behavior with NULL keys
Which statement best describes what happens when a LEFT JOIN is performed and the join key in the left table is NULL?
AThe row from the left table is included with NULLs for the right table columns.
BThe row from the left table is excluded because NULL cannot match any value.
CThe join returns an error due to NULL in the join key.
DThe row from the left table is duplicated for every row in the right table.
Attempts:
2 left
💡 Hint
Think about how LEFT JOIN preserves rows from the left table regardless of matches.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in LEFT JOIN query
Which option contains a syntax error in the LEFT JOIN SQL query?
SQL
SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.ID;
ASELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d ON e.DepartmentID = d.ID WHERE d.ID IS NOT NULL;
BSELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d ON e.DepartmentID = d.ID;
C;DI.d = DItnemtrapeD.e NO d stnemtrapeD NIOJ TFEL e seeyolpmE MORF emaNtnemtrapeD.d ,emaN.e TCELES
DSELECT e.Name, d.DepartmentName FROM Employees e LEFT JOIN Departments d e.DepartmentID = d.ID;
Attempts:
2 left
💡 Hint
Check the ON clause syntax carefully.
optimization
advanced
2:00remaining
Optimizing LEFT JOIN for large tables
You have two large tables, Employees and Departments. Which approach optimizes the LEFT JOIN query performance best?
AEnsure indexes exist on Employees.DepartmentID and Departments.ID before running the LEFT JOIN.
BUse SELECT * to retrieve all columns to avoid specifying columns.
CRun the LEFT JOIN without any indexes and add indexes after query execution.
DUse a CROSS JOIN instead of LEFT JOIN for faster results.
Attempts:
2 left
💡 Hint
Indexes help the database find matching rows faster.
🔧 Debug
expert
2:30remaining
Why does this LEFT JOIN query exclude some left rows?
Consider this query:

SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.ID
WHERE d.DepartmentName = 'HR';

Why does this query exclude employees without a department or with a department other than 'HR'?
ABecause the ON clause is incorrect and filters rows before the join.
BBecause the WHERE clause filters out rows where d.DepartmentName is NULL, removing unmatched left rows.
CBecause LEFT JOIN automatically excludes rows without matches in the right table.
DBecause the query syntax is invalid and causes an error.
Attempts:
2 left
💡 Hint
Think about how WHERE affects rows after the join.

Practice

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

Solution

  1. Step 1: Understand LEFT JOIN behavior

    A LEFT JOIN returns all rows from the left table regardless of matches in the right table.
  2. Step 2: Check what happens with unmatched rows

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

    Keeps all rows from the left table and adds matching rows from the right table or NULL if no match. -> Option D
  4. Quick Check:

    LEFT JOIN = all left rows kept [OK]
Hint: Remember: LEFT JOIN keeps all left rows, fills right with NULL if no match [OK]
Common Mistakes:
  • Confusing LEFT JOIN with INNER JOIN
  • Thinking it keeps all right table rows
  • Assuming unmatched rows are dropped
2. Which of the following is the correct syntax for a LEFT JOIN in SQL?
easy
A. SELECT * FROM table1 LEFT ON 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 ON table1.id = table2.id;
D. SELECT * FROM table1 LEFT JOIN table2 WHERE table1.id = table2.id;

Solution

  1. Step 1: Review correct LEFT JOIN syntax

    The correct syntax is: SELECT columns FROM left_table LEFT JOIN right_table ON condition;
  2. Step 2: Identify syntax errors in other options

    Options A, B, and D misuse keywords or omit ON clause, causing syntax errors.
  3. Final Answer:

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

    LEFT JOIN syntax = SELECT ... LEFT JOIN ... ON ... [OK]
Hint: LEFT JOIN always uses ON to specify join condition [OK]
Common Mistakes:
  • Swapping JOIN and LEFT keywords
  • Using WHERE instead of ON for join condition
  • Omitting ON clause
3. Given these tables:

Employees
id | name
1 | Alice
2 | Bob
3 | Carol

Sales
emp_id | amount
1 | 100
3 | 200

What is the result of:
SELECT Employees.name, Sales.amount FROM Employees LEFT JOIN Sales ON Employees.id = Sales.emp_id;
medium
A. [('Alice', 100), ('Bob', 0), ('Carol', 200)]
B. [('Alice', 100), ('Bob', NULL), ('Carol', 200)]
C. [('Alice', 100), ('Carol', 200)]
D. [('Bob', NULL)]

Solution

  1. Step 1: Match Employees with Sales using LEFT JOIN

    All Employees rows appear. For matching emp_id in Sales, amount is shown; else NULL.
  2. Step 2: Map each employee to sales amount or NULL

    Alice (id=1) matches 100, Bob (id=2) no match so NULL, Carol (id=3) matches 200.
  3. Final Answer:

    [('Alice', 100), ('Bob', NULL), ('Carol', 200)] -> Option B
  4. Quick Check:

    LEFT JOIN keeps all left rows with NULL for no match [OK]
Hint: LEFT JOIN shows NULL for unmatched right rows, not zero [OK]
Common Mistakes:
  • Replacing NULL with zero
  • Omitting unmatched rows
  • Confusing LEFT JOIN with INNER JOIN
4. Consider this query:

SELECT a.id, b.value FROM A LEFT JOIN B ON a.id = b.a_id WHERE b.value > 10;

What is the problem with this query if you want to keep all rows from A?
medium
A. The WHERE clause filters out rows where b.value is NULL, losing some left rows.
B. The ON clause is missing a join condition.
C. LEFT JOIN should be replaced with INNER JOIN for correct results.
D. The SELECT statement is missing table aliases.

Solution

  1. Step 1: Understand effect of WHERE on LEFT JOIN

    WHERE filters after join, so rows with NULL b.value are removed, losing left rows.
  2. Step 2: Identify how to fix to keep all left rows

    Move condition to ON clause or use WHERE b.value > 10 OR b.value IS NULL to preserve unmatched rows.
  3. Final Answer:

    The WHERE clause filters out rows where b.value is NULL, losing some left rows. -> Option A
  4. Quick Check:

    WHERE after LEFT JOIN can remove unmatched rows [OK]
Hint: Put filters on right table in ON, not WHERE, to keep all left rows [OK]
Common Mistakes:
  • Assuming WHERE doesn't affect LEFT JOIN results
  • Confusing ON and WHERE clauses
  • Replacing LEFT JOIN with INNER JOIN unnecessarily
5. You have two tables:

Products
product_id | name
1 | Pen
2 | Pencil
3 | Eraser

Sales
product_id | quantity
1 | 10
1 | 5
3 | 7

Write a query using LEFT JOIN to get each product's total sales quantity, showing 0 if no sales exist.
hard
A. SELECT p.name, COALESCE(SUM(s.quantity), 0) AS total FROM Products p LEFT JOIN Sales s ON p.product_id = s.product_id GROUP BY p.name;
B. SELECT p.name, SUM(s.quantity) AS total FROM Products p INNER JOIN Sales s ON p.product_id = s.product_id GROUP BY p.name;
C. SELECT p.name, SUM(s.quantity) AS total FROM Products p LEFT JOIN Sales s ON p.product_id = s.product_id WHERE s.quantity > 0 GROUP BY p.name;
D. SELECT p.name, SUM(s.quantity) AS total FROM Sales s LEFT JOIN Products p ON s.product_id = p.product_id GROUP BY p.name;

Solution

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

    LEFT JOIN ensures all products appear even if no sales exist.
  2. Step 2: Use COALESCE with SUM to show 0 for no sales

    SUM returns NULL if no matching rows; COALESCE converts NULL to 0.
  3. Final Answer:

    SELECT p.name, COALESCE(SUM(s.quantity), 0) AS total FROM Products p LEFT JOIN Sales s ON p.product_id = s.product_id GROUP BY p.name; -> Option A
  4. Quick Check:

    LEFT JOIN + COALESCE(SUM()) = total sales with zeros [OK]
Hint: Use COALESCE(SUM()) with LEFT JOIN to replace NULL totals with zero [OK]
Common Mistakes:
  • Using INNER JOIN losing products with no sales
  • Not handling NULL sums with COALESCE
  • Filtering in WHERE removing unmatched rows