NULLs in JOIN conditions in SQL - Time & Space Complexity
When we join tables in a database, the time it takes depends on how many rows we compare. NULL values in join conditions can affect which rows match, but how does this impact the work done?
We want to understand how the presence of NULLs in join conditions affects the number of operations the database performs.
Analyze the time complexity of the following SQL join with NULLs in the condition.
SELECT a.id, b.id
FROM tableA a
LEFT JOIN tableB b
ON a.key = b.key
OR (a.key IS NULL AND b.key IS NULL);
This query joins two tables on a key column, treating NULLs as equal to each other in the join condition.
Look for repeated comparisons between rows.
- Primary operation: Comparing each row in
tableAto rows intableBto find matches. - How many times: Potentially every row in
tableAchecks against many rows intableB, especially if no indexes help.
As the number of rows grows, the comparisons increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The work grows quickly as the tables get bigger, roughly multiplying the number of rows in both tables.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the number of rows in the first table by the number of rows in the second table.
[X] Wrong: "Adding NULL checks in the join condition makes the query faster because it filters rows early."
[OK] Correct: The NULL checks add complexity to each comparison and do not reduce the number of comparisons, so the total work can stay the same or increase.
Understanding how NULLs affect join conditions helps you explain query performance clearly. This skill shows you can think about how databases work behind the scenes, which is valuable in many real projects.
What if we replaced the OR condition with a COALESCE function to handle NULLs? How would that change the time complexity?
Practice
INNER JOIN on columns that contain NULL values in SQL?Solution
Step 1: Understand how
When joining tables, SQL compares join columns usingINNER JOINworks with=condition=. If a column hasNULL, it means unknown, so comparison fails.Step 2: Effect of
SinceNULLin join columnsNULL = NULLis not true, rows withNULLin join columns do not match and are excluded.Final Answer:
Rows withNULLin join columns are excluded from the result. -> Option BQuick Check:
NULLmeans unknown, so no match [OK]
- Assuming NULL equals NULL in join conditions
- Expecting NULL rows to appear in INNER JOIN results
- Thinking NULL causes query errors in JOIN
NULL values in the join column from the left table?Solution
Step 1: Identify JOIN types that keep left table rows
LEFT JOINkeeps all rows from the left table, including those withNULLin join columns.Step 2: Check other options
INNER JOINexcludes unmatched rows;RIGHT JOINkeeps right table rows;FULL JOINkeeps all but filtering withWHERE A.id IS NOT NULLremoves left table NULLs.Final Answer:
SELECT * FROM A LEFT JOIN B ON A.id = B.id;-> Option CQuick Check:
LEFT JOIN keeps left rows even if join column is NULL [OK]
- Using INNER JOIN expecting NULL rows to appear
- Filtering NULLs after FULL JOIN with WHERE clause
- Confusing RIGHT JOIN with LEFT JOIN behavior
Table A: id1
2
NULL
Table B: id2
NULL
What is the result of this query?
SELECT A.id, B.id FROM A INNER JOIN B ON A.id = B.id;
Solution
Step 1: Understand INNER JOIN with NULLs
INNER JOIN matches rows whereA.id = B.id. NULLs do not match anything, even NULL.Step 2: Check matching rows
OnlyA.id = 2matchesB.id = 2. Rows with NULL do not match.Final Answer:
(2, 2) -> Option DQuick Check:
NULLs do not match, only 2 joins [OK]
- Including NULL = NULL as a match
- Assuming all rows join regardless of NULL
- Confusing INNER JOIN with OUTER JOIN behavior
SELECT * FROM A LEFT JOIN B ON A.id = B.id WHERE B.id IS NOT NULL;
But it returns no rows, even though you expect rows from A with NULL
id. What is the likely problem?Solution
Step 1: Understand LEFT JOIN with WHERE filter
LEFT JOIN keeps all A rows, butWHERE B.id IS NOT NULLfilters only rows where B.id is NOT NULL after join.Step 2: Effect on rows with NULL in A.id
Rows with NULL in A.id do not match B.id, so B.id is NULL after the join. TheWHERE B.id IS NOT NULLcondition excludes these rows.Final Answer:
TheWHERE B.id IS NOT NULLfilters out all rows including those with NULL in A.id. -> Option AQuick Check:
WHERE filters after join, can exclude expected rows [OK]
- Thinking LEFT JOIN excludes NULL join columns
- Using INNER JOIN expecting NULL rows
- Assuming join condition causes syntax error
id columns, but include rows where id is NULL in either table. Which query correctly returns all rows from both tables, matching on id including NULLs treated as equal?Solution
Step 1: Understand FULL JOIN and NULL behavior
FULL JOIN returns all rows from both tables, butA.id = B.idexcludes NULL matches because NULL = NULL is false.Step 2: Add explicit NULL check in join condition
UsingOR (A.id IS NULL AND B.id IS NULL)treats NULLs as equal, including those rows in join results.Step 3: Check other options
SELECT * FROM A FULL JOIN B ON A.id = B.id;
excludes NULL matches;SELECT * FROM A INNER JOIN B ON A.id = B.id OR (A.id IS NULL AND B.id IS NULL);
uses INNER JOIN, excluding unmatched rows;SELECT * FROM A LEFT JOIN B ON A.id = B.id WHERE A.id IS NOT NULL;
filters out NULLs.Final Answer:
SELECT * FROM A FULL JOIN B ON A.id = B.id OR (A.id IS NULL AND B.id IS NULL); -> Option AQuick Check:
Explicit NULL checks in JOIN condition include NULL matches [OK]
- Assuming NULL = NULL matches automatically
- Using INNER JOIN instead of FULL JOIN for all rows
- Filtering NULLs unintentionally with WHERE clause
Ready to prove it?
Pass the test and this pattern moves unknown
