Finding unmatched rows with LEFT JOIN in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use a LEFT JOIN to find rows in one table that don't have matches in another, we want to know how the work grows as the tables get bigger.
How does the number of rows affect the time it takes to find unmatched rows?
Analyze the time complexity of the following code snippet.
SELECT a.id
FROM TableA a
LEFT JOIN TableB b ON a.id = b.a_id
WHERE b.a_id IS NULL;
This query finds all rows in TableA that do not have a matching row in TableB.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row in TableA, the database looks for matching rows in TableB.
- How many times: This happens once for every row in TableA.
As TableA grows, the database must check more rows to find unmatched ones.
| Input Size (rows in TableA) | Approx. Operations |
|---|---|
| 10 | About 10 lookups in TableB |
| 100 | About 100 lookups in TableB |
| 1000 | About 1000 lookups in TableB |
Pattern observation: The work grows roughly in direct proportion to the number of rows in TableA.
Time Complexity: O(n)
This means the time to find unmatched rows grows linearly with the number of rows in the first table.
[X] Wrong: "The LEFT JOIN will check every combination of rows in both tables, so it's much slower than it really is."
[OK] Correct: The database uses indexes or efficient search methods to avoid checking every pair, so it doesn't do a full cross-check of all rows.
Understanding how joins scale helps you explain query performance clearly and shows you know how databases handle matching and missing data efficiently.
"What if we added an index on TableB.a_id? How would the time complexity change?"
Practice
LEFT JOIN combined with WHERE right_table.key IS NULL do in SQL?Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN returns all rows from the left table and matching rows from the right table. If no match exists, right table columns are NULL.Step 2: Apply WHERE condition to filter unmatched rows
Filtering withWHERE right_table.key IS NULLselects only those left table rows without a match in the right table.Final Answer:
Finds rows in the left table that have no matching rows in the right table -> Option BQuick Check:
LEFT JOIN + IS NULL = unmatched left rows [OK]
- Confusing unmatched rows as from right table
- Using INNER JOIN instead of LEFT JOIN
- Not checking for NULL in right table columns
Solution
Step 1: Identify correct JOIN type
LEFT JOIN keeps all customers and matches orders; unmatched orders will be NULL.Step 2: Filter unmatched orders
WHEREo.customer_id IS NULLselects customers without orders.Final Answer:
SELECT c.id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.customer_id IS NULL; -> Option DQuick Check:
LEFT JOIN + right table NULL = unmatched left rows [OK]
- Using INNER JOIN which excludes unmatched rows
- Checking NULL on left table columns
- Using RIGHT JOIN incorrectly for this case
employees(id, name) and tasks(employee_id, task_name), what does this query return?SELECT e.name FROM employees e LEFT JOIN tasks t ON e.id = t.employee_id WHERE t.employee_id IS NULL;
Solution
Step 1: Analyze LEFT JOIN and ON condition
The query joins employees with tasks on employee ID, keeping all employees.Step 2: Filter rows where tasks are missing
WHEREt.employee_id IS NULLselects employees with no matching tasks.Final Answer:
Names of employees who have no tasks assigned -> Option AQuick Check:
LEFT JOIN + NULL in right table = unmatched left rows [OK]
- Thinking it returns employees with tasks
- Confusing NULL check on left table
- Assuming INNER JOIN behavior
SELECT p.product_id FROM products p LEFT JOIN sales s ON p.product_id = s.product_id WHERE p.product_id IS NULL;
Solution
Step 1: Understand LEFT JOIN result
LEFT JOIN keeps all products; unmatched sales columns are NULL.Step 2: Check WHERE clause correctness
Filtering onp.product_id IS NULLis wrong because left table columns are never NULL in LEFT JOIN; should checks.product_id IS NULL.Final Answer:
The WHERE clause should check s.product_id IS NULL, not p.product_id -> Option CQuick Check:
Filter NULL on right table columns, not left [OK]
- Checking NULL on left table columns
- Using INNER JOIN instead of LEFT JOIN
- Incorrect ON join condition
students(id, name) and enrollments(student_id, course_id). Write a query to find students not enrolled in any course, considering some students may have NULL IDs. Which query correctly handles this?Solution
Step 1: Use LEFT JOIN to find unmatched students
LEFT JOIN students with enrollments keeps all students; unmatched enrollments are NULL.Step 2: Filter students without enrollments and exclude NULL student IDs
WHEREe.student_id IS NULLfinds students without courses; addings.id IS NOT NULLexcludes students with NULL IDs to avoid incorrect matches.Final Answer:
SELECT s.name FROM students s LEFT JOIN enrollments e ON s.id = e.student_id WHERE e.student_id IS NULL AND s.id IS NOT NULL; -> Option AQuick Check:
LEFT JOIN + right NULL + exclude NULL left keys = correct unmatched [OK]
- Ignoring NULL IDs in left table
- Using INNER JOIN which excludes unmatched rows
- Using RIGHT JOIN incorrectly
