0
0
SQLquery~20 mins

NULLs in JOIN conditions in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULL Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of INNER JOIN with NULL values in join columns

Consider two tables Employees and Departments:

Employees:
id | name | dept_id
1 | Alice | 10
2 | Bob | NULL
3 | Carol | 20

Departments:
dept_id | dept_name
10 | Sales
20 | HR
NULL | Unknown

What will be the result of this query?

SELECT e.name, d.dept_name
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id;
SQL
SELECT e.name, d.dept_name
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id;
A[{"name": "Alice", "dept_name": "Sales"}, {"name": "Carol", "dept_name": "HR"}]
B[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Unknown"}, {"name": "Carol", "dept_name": "HR"}]
C[{"name": "Bob", "dept_name": "Unknown"}]
D[]
Attempts:
2 left
💡 Hint

Remember that = comparison with NULL does not return true in SQL.

query_result
intermediate
2:00remaining
LEFT JOIN behavior with NULLs in join columns

Using the same tables Employees and Departments as before, what is the output of this query?

SELECT e.name, d.dept_name
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id;
SQL
SELECT e.name, d.dept_name
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id;
A[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": null}, {"name": "Carol", "dept_name": "HR"}]
B[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "Unknown"}, {"name": "Carol", "dept_name": "HR"}]
C[{"name": "Bob", "dept_name": null}]
D[]
Attempts:
2 left
💡 Hint

LEFT JOIN keeps all rows from the left table, even if no match is found on the right.

🧠 Conceptual
advanced
1:30remaining
Why does NULL in join condition cause no match?

Why does a join condition like table1.col = table2.col fail to match rows when either column contains NULL?

ABecause NULL is treated as a special number that matches only itself.
BBecause NULL is treated as zero, so it only matches zero values.
CBecause NULL is treated as an empty string, so it only matches empty strings.
DBecause NULL is treated as an unknown value, and comparisons with unknown always return false in SQL.
Attempts:
2 left
💡 Hint

Think about how SQL treats NULL in logical comparisons.

📝 Syntax
advanced
1:30remaining
Correct syntax to join on NULL-safe equality

Which SQL join condition correctly matches rows where columns may be NULL, treating NULLs as equal?

AON e.dept_id <> d.dept_id
BON e.dept_id = d.dept_id AND e.dept_id IS NOT NULL
CON e.dept_id = d.dept_id OR (e.dept_id IS NULL AND d.dept_id IS NULL)
DON e.dept_id IS NULL AND d.dept_id IS NULL
Attempts:
2 left
💡 Hint

Think about how to explicitly check for NULL equality in SQL.

optimization
expert
3:00remaining
Optimizing JOIN with NULL-safe condition for large tables

You have two large tables with millions of rows each, and you want to join them on columns that may contain NULLs, treating NULLs as equal. Which approach is most efficient?

AUse CROSS JOIN and filter matching rows with NULL-safe condition in WHERE clause.
BUse <code>ON COALESCE(t1.col, -1) = COALESCE(t2.col, -1)</code> assuming -1 is not a valid value.
CUse a FULL OUTER JOIN and filter rows with matching or both NULL columns in WHERE clause.
DUse <code>ON t1.col = t2.col OR (t1.col IS NULL AND t2.col IS NULL)</code> with proper indexes on the columns.
Attempts:
2 left
💡 Hint

Consider how indexes and functions affect query performance.