Bird
0
0

What is the logical issue in this query?

medium📝 Debug Q7 of 15
SQL - LEFT and RIGHT JOIN
What is the logical issue in this query?
SELECT e.name, d.dept_name FROM employees e LEFT JOIN departments d ON e.dept_id = d.id LEFT JOIN projects p ON p.dept_id = d.id WHERE p.project_name = 'Alpha';
AThe WHERE clause filters out rows where projects are NULL, negating the LEFT JOIN effect.
BThe JOIN condition between employees and departments is incorrect.
CThe query should use INNER JOIN instead of LEFT JOIN for departments.
DThe SELECT clause is missing the project name.
Step-by-Step Solution
Solution:
  1. Step 1: Understand LEFT JOIN with WHERE

    LEFT JOIN preserves all left table rows, but WHERE filters after joins.
  2. Step 2: Analyze WHERE clause effect

    Filtering on p.project_name = 'Alpha' excludes rows where p is NULL, effectively converting LEFT JOIN to INNER JOIN.
  3. Final Answer:

    The WHERE clause filters out rows where projects are NULL, negating the LEFT JOIN effect. -> Option A
  4. Quick Check:

    WHERE on right table columns can nullify LEFT JOIN [OK]
Quick Trick: WHERE on right table columns removes unmatched rows [OK]
Common Mistakes:
MISTAKES
  • Assuming LEFT JOIN always preserves all left rows
  • Placing filtering conditions on right table columns in WHERE
  • Confusing JOIN conditions with filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes