Bird
0
0

Given these tables:

hard📝 Application Q9 of 15
SQL - INNER JOIN
Given these tables:

Employees
EmpID | Name | ManagerID
1 | John | NULL
2 | Jane | 1
3 | Dave | 1

Departments
DeptID | ManagerID | DeptName
10 | 1 | Sales
20 | 2 | Marketing

Write a query to find employee names and their department names by joining Employees and Departments on the manager relationship.
Which query is correct?
ASELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.EmpID = Departments.ManagerID;
BSELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.ManagerID = Departments.DeptID;
CSELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.ManagerID = Departments.ManagerID;
DSELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.EmpID = Departments.DeptID;
Step-by-Step Solution
Solution:
  1. Step 1: Understand join condition

    Departments.ManagerID matches Employees.EmpID to link managers to departments.
  2. Step 2: Check each ON condition

    SELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.EmpID = Departments.ManagerID; correctly joins Employees.EmpID to Departments.ManagerID; others mismatch columns.
  3. Final Answer:

    SELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.EmpID = Departments.ManagerID; -> Option A
  4. Quick Check:

    Join on manager ID links employees to departments [OK]
Quick Trick: Match manager IDs to join employees and departments [OK]
Common Mistakes:
MISTAKES
  • Joining wrong columns like ManagerID to DeptID
  • Confusing employee and department keys
  • Using NULL values in join condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes