Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you use an INNER JOIN on columns that contain NULL values in SQL?
easy
A. Rows with NULL in join columns are always included.
B. Rows with NULL in join columns are excluded from the result.
C. The query returns an error due to NULL values.
D. Rows with NULL are treated as equal and joined together.

Solution

  1. Step 1: Understand how INNER JOIN works with = condition

    When joining tables, SQL compares join columns using =. If a column has NULL, it means unknown, so comparison fails.
  2. Step 2: Effect of NULL in join columns

    Since NULL = NULL is not true, rows with NULL in join columns do not match and are excluded.
  3. Final Answer:

    Rows with NULL in join columns are excluded from the result. -> Option B
  4. Quick Check:

    NULL means unknown, so no match [OK]
Hint: Remember: NULL never equals NULL in joins [OK]
Common Mistakes:
  • Assuming NULL equals NULL in join conditions
  • Expecting NULL rows to appear in INNER JOIN results
  • Thinking NULL causes query errors in JOIN
2. Which of the following SQL JOIN syntaxes correctly includes rows with NULL values in the join column from the left table?
easy
A. SELECT * FROM A INNER JOIN B ON A.id = B.id;
B. SELECT * FROM A RIGHT JOIN B ON A.id = B.id;
C. SELECT * FROM A LEFT JOIN B ON A.id = B.id;
D. SELECT * FROM A FULL JOIN B ON A.id = B.id WHERE A.id IS NOT NULL;

Solution

  1. Step 1: Identify JOIN types that keep left table rows

    LEFT JOIN keeps all rows from the left table, including those with NULL in join columns.
  2. Step 2: Check other options

    INNER JOIN excludes unmatched rows; RIGHT JOIN keeps right table rows; FULL JOIN keeps all but filtering with WHERE A.id IS NOT NULL removes left table NULLs.
  3. Final Answer:

    SELECT * FROM A LEFT JOIN B ON A.id = B.id; -> Option C
  4. Quick Check:

    LEFT JOIN keeps left rows even if join column is NULL [OK]
Hint: Use LEFT JOIN to keep left table NULL rows [OK]
Common Mistakes:
  • Using INNER JOIN expecting NULL rows to appear
  • Filtering NULLs after FULL JOIN with WHERE clause
  • Confusing RIGHT JOIN with LEFT JOIN behavior
3. Given tables:
Table A: id
1
2
NULL

Table B: id
2
NULL

What is the result of this query?
SELECT A.id, B.id FROM A INNER JOIN B ON A.id = B.id;
medium
A. (1, 2), (2, 2), (NULL, NULL)
B. (NULL, NULL)
C. (1, 2), (2, 2)
D. (2, 2)

Solution

  1. Step 1: Understand INNER JOIN with NULLs

    INNER JOIN matches rows where A.id = B.id. NULLs do not match anything, even NULL.
  2. Step 2: Check matching rows

    Only A.id = 2 matches B.id = 2. Rows with NULL do not match.
  3. Final Answer:

    (2, 2) -> Option D
  4. Quick Check:

    NULLs do not match, only 2 joins [OK]
Hint: NULLs never match in INNER JOIN conditions [OK]
Common Mistakes:
  • Including NULL = NULL as a match
  • Assuming all rows join regardless of NULL
  • Confusing INNER JOIN with OUTER JOIN behavior
4. You wrote this query:
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?
medium
A. The WHERE B.id IS NOT NULL filters out all rows including those with NULL in A.id.
B. LEFT JOIN does not include rows with NULL in join columns.
C. You should use INNER JOIN instead of LEFT JOIN.
D. The join condition A.id = B.id causes a syntax error.

Solution

  1. Step 1: Understand LEFT JOIN with WHERE filter

    LEFT JOIN keeps all A rows, but WHERE B.id IS NOT NULL filters only rows where B.id is NOT NULL after join.
  2. 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. The WHERE B.id IS NOT NULL condition excludes these rows.
  3. Final Answer:

    The WHERE B.id IS NOT NULL filters out all rows including those with NULL in A.id. -> Option A
  4. Quick Check:

    WHERE filters after join, can exclude expected rows [OK]
Hint: Remember WHERE filters after JOIN, can exclude NULL rows [OK]
Common Mistakes:
  • Thinking LEFT JOIN excludes NULL join columns
  • Using INNER JOIN expecting NULL rows
  • Assuming join condition causes syntax error
5. You want to join tables A and B on 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?
hard
A.
SELECT * FROM A FULL JOIN B ON A.id = B.id OR (A.id IS NULL AND B.id IS NULL);
B.
SELECT * FROM A FULL JOIN B ON A.id = B.id;
C.
SELECT * FROM A INNER JOIN B ON A.id = B.id OR (A.id IS NULL AND B.id IS NULL);
D.
SELECT * FROM A LEFT JOIN B ON A.id = B.id WHERE A.id IS NOT NULL;

Solution

  1. Step 1: Understand FULL JOIN and NULL behavior

    FULL JOIN returns all rows from both tables, but A.id = B.id excludes NULL matches because NULL = NULL is false.
  2. Step 2: Add explicit NULL check in join condition

    Using OR (A.id IS NULL AND B.id IS NULL) treats NULLs as equal, including those rows in join results.
  3. 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.
  4. Final Answer:

    SELECT * FROM A FULL JOIN B ON A.id = B.id OR (A.id IS NULL AND B.id IS NULL); -> Option A
  5. Quick Check:

    Explicit NULL checks in JOIN condition include NULL matches [OK]
Hint: Add IS NULL checks in JOIN to match NULLs explicitly [OK]
Common Mistakes:
  • Assuming NULL = NULL matches automatically
  • Using INNER JOIN instead of FULL JOIN for all rows
  • Filtering NULLs unintentionally with WHERE clause
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard