Bird
0
0

Given tables Employees and Departments with data:

medium📝 query result Q13 of 15
SQL - LEFT and RIGHT JOIN
Given tables Employees and Departments with data:

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

Departments:
dept_id | dept_name
10 | Sales
20 | HR
30 | IT

What is the result of this query?
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": "HR"}]
B[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": null}]
C[{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": "IT"}]
D[{"name": "Alice", "dept_name": null}, {"name": "Bob", "dept_name": null}, {"name": "Carol", "dept_name": null}]
Step-by-Step Solution
Solution:
  1. Step 1: Match Employees with Departments by dept_id

    Alice's dept_id 10 matches Sales, Bob's 20 matches HR, Carol's NULL has no match.
  2. Step 2: Apply LEFT JOIN behavior

    All employees appear. For Carol, no matching department, so dept_name is NULL.
  3. Final Answer:

    [{"name": "Alice", "dept_name": "Sales"}, {"name": "Bob", "dept_name": "HR"}, {"name": "Carol", "dept_name": null}] -> Option B
  4. Quick Check:

    LEFT JOIN keeps all left rows, unmatched right columns NULL [OK]
Quick Trick: LEFT JOIN shows NULL for unmatched right table rows [OK]
Common Mistakes:
MISTAKES
  • Omitting rows with NULL join keys
  • Assuming unmatched rows get default values
  • Confusing INNER JOIN output with LEFT JOIN

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes