Hint: Match manager_id to id with LEFT JOIN for manager names [OK]
Common Mistakes:
Confusing employee and manager columns
Using INNER JOIN losing employees without managers
Assuming manager_id is employee id
4. Consider this query intended to find pairs of employees who share the same manager:
SELECT e1.name, e2.name
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.manager_id
WHERE e1.id != e2.id;
What is the main issue with this query?
medium
A. It will return duplicate pairs in both orders (e.g., (Alice, Bob) and (Bob, Alice))
B. It will not return any rows because of the WHERE clause
C. It causes a syntax error due to missing aliases
D. It only returns employees with no manager
Solution
Step 1: Analyze join condition
The join matches employees with the same manager_id, including reversed pairs.
Step 2: Check WHERE clause effect
WHERE e1.id != e2.id avoids self-pairing but does not prevent duplicate pairs in reverse order.
Final Answer:
It will return duplicate pairs in both orders (e.g., (Alice, Bob) and (Bob, Alice)) -> Option A
Quick Check:
Self join can produce reversed duplicate pairs [OK]
Hint: Self join pairs may appear twice reversed; use filtering [OK]
Common Mistakes:
Assuming WHERE clause removes duplicates
Thinking query causes syntax error
Believing it returns only employees without managers
5. You want to find all employees who are managers themselves, i.e., employees who have at least one other employee reporting to them. Which self join query correctly achieves this?
hard
A. SELECT e1.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.id;
B. SELECT e1.name FROM employees e1 LEFT JOIN employees e2 ON e1.id = e2.manager_id WHERE e2.id IS NULL;
C. SELECT DISTINCT e1.name FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id;
D. SELECT e1.name FROM employees e1 WHERE e1.manager_id IS NOT NULL;
Solution
Step 1: Understand the requirement
Managers are employees whose id appears as manager_id for others.
Step 2: Analyze each option
SELECT DISTINCT e1.name FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id; joins employees where e1.id = e2.manager_id, selecting distinct managers correctly.
Final Answer:
SELECT DISTINCT e1.name FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id; -> Option C
Quick Check:
Join on id = manager_id finds managers [OK]
Hint: Join employees where id = manager_id to find managers [OK]
Common Mistakes:
Using LEFT JOIN with IS NULL returns non-managers
Joining on wrong columns (manager_id = id reversed)
Filtering on manager_id IS NOT NULL returns employees with managers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.