LEFT JOIN with NULL result rows in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using a LEFT JOIN in SQL, it is important to understand how the query's work grows as the tables get bigger.
We want to know how the time to run the query changes when the input tables have more rows.
Analyze the time complexity of the following code snippet.
SELECT a.id, b.value
FROM tableA a
LEFT JOIN tableB b ON a.id = b.a_id
WHERE b.value IS NULL;
This query finds all rows in tableA that do not have matching rows in tableB.
- Primary operation: For each row in tableA, the database looks for matching rows in tableB.
- How many times: This matching happens once per row in tableA.
As tableA grows, the number of lookups into tableB grows proportionally.
| Input Size (n) | 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 directly with the number of rows in tableA.
Time Complexity: O(n)
This means the time to run the query grows roughly in direct proportion to the size of tableA.
[X] Wrong: "The LEFT JOIN will take time proportional to the product of both tables' sizes because it compares every row to every other row."
[OK] Correct: The database uses indexes or efficient lookups, so it does not check every pair. It mainly scans tableA and looks up matches in tableB, not all combinations.
Understanding how JOINs scale helps you write queries that run well on large data. This skill shows you can think about performance, not just correctness.
"What if we changed the LEFT JOIN to an INNER JOIN? How would the time complexity change?"
Practice
LEFT JOIN do in SQL?Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN keeps all rows from the left table regardless of matches in the right table.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.Final Answer:
Returns all rows from the left table and matched rows from the right table, NULL if no match. -> Option AQuick Check:
LEFT JOIN = all left rows + NULL for no match [OK]
- Confusing LEFT JOIN with INNER JOIN
- Thinking unmatched rows are dropped
- Assuming NULLs appear in left table columns
Solution
Step 1: Review standard LEFT JOIN syntax
The correct syntax is: SELECT columns FROM left_table LEFT JOIN right_table ON condition.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.Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option DQuick Check:
LEFT JOIN syntax = LEFT JOIN ... ON ... [OK]
- Swapping JOIN and LEFT keywords
- Using WHERE instead of ON for join condition
- Omitting parentheses in USING clause
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;
Solution
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.Step 2: Apply LEFT JOIN behavior
All employees appear. For Carol, no matching department, so dept_name is NULL.Final Answer:
[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": null}] -> Option BQuick Check:
LEFT JOIN keeps all left rows, unmatched right columns NULL [OK]
- Omitting rows with NULL join keys
- Assuming unmatched rows get default values
- Confusing INNER JOIN output with LEFT JOIN
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?
Solution
Step 1: Understand LEFT JOIN with WHERE filter
LEFT JOIN keeps all rows from A, but WHERE filters after join.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.Final Answer:
Because the WHERE clause filters out rows where b.value is NULL, removing unmatched rows. -> Option CQuick Check:
WHERE filters NULLs after LEFT JOIN, reducing rows [OK]
- Thinking LEFT JOIN always keeps all left rows regardless of WHERE
- Confusing ON and WHERE filtering effects
- Assuming query syntax error causes fewer rows
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?
Solution
Step 1: Use LEFT JOIN to keep all orders
LEFT JOIN keeps all orders even if no matching customer exists.Step 2: Replace NULL customer names with 'Unknown'
Use COALESCE to show 'Unknown' when c.name is NULL.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 AQuick Check:
LEFT JOIN + COALESCE handles missing customers [OK]
- Using INNER JOIN excludes orders without customers
- Using RIGHT JOIN reverses table roles incorrectly
- Forgetting to handle NULL customer names
