Bird
0
0

Identify the mistake in this query intended to find unmatched records:

medium📝 Debug Q6 of 15
SQL - LEFT and RIGHT JOIN
Identify the mistake in this query intended to find unmatched records:
SELECT c.id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE c.id IS NULL;
AThe query should use INNER JOIN instead of LEFT JOIN
BThe JOIN condition is incorrect; it should join on order_id
CThe WHERE clause should check for NULL in the right table's key, not the left table's
DThe SELECT clause should include columns from the orders table
Step-by-Step Solution
Solution:
  1. Step 1: LEFT JOIN returns all left rows

    Rows from customers (left) are always present.
  2. Step 2: WHERE c.id IS NULL

    This condition filters out all rows because c.id is never NULL in the left table.
  3. Step 3: Correct NULL check

    To find unmatched customers, check for NULL in the right table's key (o.customer_id).
  4. Final Answer:

    The WHERE clause should check for NULL in the right table's key, not the left table's -> Option C
  5. Quick Check:

    Check NULL on right table key after LEFT JOIN to find unmatched rows [OK]
Quick Trick: Check NULL on right table key, not left table [OK]
Common Mistakes:
MISTAKES
  • Checking NULL on left table columns after LEFT JOIN
  • Using INNER JOIN to find unmatched rows
  • Incorrect join condition on unrelated columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes