0
0
SQLquery~5 mins

NULLs in JOIN conditions in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What happens when a NULL value is used in a JOIN condition?
In SQL, NULL represents an unknown value. When used in a JOIN condition, NULL does not match any value, including another NULL. This means rows with NULL in the join columns are usually excluded from the join result.
Click to reveal answer
beginner
Why do NULLs cause rows to be excluded in INNER JOINs?
INNER JOIN returns rows where the join condition is true. Since NULL compared to any value (even NULL) is unknown (not true), rows with NULL in join columns do not satisfy the condition and are excluded.
Click to reveal answer
intermediate
How can you include rows with NULLs in join columns when joining tables?
You can use LEFT JOIN or RIGHT JOIN to include rows with NULLs from one side. Also, you can use conditions like ON t1.col = t2.col OR (t1.col IS NULL AND t2.col IS NULL) to treat NULLs as equal in join conditions.
Click to reveal answer
beginner
What is the difference between NULL and empty string ('') in JOIN conditions?
NULL means unknown or missing value, while empty string is a known value with zero length. In JOINs, NULL does not match anything, but empty string matches another empty string exactly.
Click to reveal answer
intermediate
Write a JOIN condition that treats NULLs as equal in SQL.
Example:
SELECT * FROM table1 t1 JOIN table2 t2 ON (t1.col = t2.col OR (t1.col IS NULL AND t2.col IS NULL));
This condition matches rows where columns are equal or both NULL.
Click to reveal answer
What does an INNER JOIN do with rows where the join column is NULL?
AExcludes them because NULL does not equal anything
BConverts NULL to zero and joins
CIncludes them always
DIncludes them if both sides have NULL
Which JOIN type can include rows with NULL in the join column from one table?
AINNER JOIN
BLEFT JOIN
CFULL JOIN
DCROSS JOIN
How can you write a join condition to treat NULLs as equal?
AUse <code>t1.col = t2.col</code> only
BUse <code>t1.col IS NULL OR t2.col IS NULL</code>
CUse <code>t1.col = t2.col OR (t1.col IS NULL AND t2.col IS NULL)</code>
DUse <code>t1.col != t2.col</code>
What is the result of comparing NULL = NULL in SQL?
AUNKNOWN (treated as FALSE in JOIN)
BFALSE
CTRUE
DError
Which of these is true about NULL in JOIN conditions?
ANULL is converted to zero automatically
BNULL matches NULL by default
CNULL matches empty string
DNULL never matches any value including NULL
Explain why NULL values cause rows to be excluded in INNER JOINs and how to include them.
Think about how SQL treats NULL in comparisons and how different JOIN types behave.
You got /5 concepts.
    Describe how to write a JOIN condition that treats NULL values as equal and why this might be useful.
    Consider how NULLs are normally excluded and how to override that.
    You got /4 concepts.