Bird
0
0

Given the employees(id, name, manager_id) table, which query correctly lists all employees who manage at least one other employee?

hard📝 Application Q8 of 15
PostgreSQL - Joins in PostgreSQL
Given the employees(id, name, manager_id) table, which query correctly lists all employees who manage at least one other employee?
ASELECT e.name FROM employees e WHERE e.manager_id IS NULL;
BSELECT DISTINCT m.name FROM employees m JOIN employees e ON m.id = e.manager_id;
CSELECT e.name FROM employees e JOIN employees m ON e.id = m.manager_id WHERE e.id = m.id;
DSELECT name FROM employees WHERE manager_id = id;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want employees who appear as managers (id matches manager_id of others).
  2. Step 2: Analyze options

    SELECT DISTINCT m.name FROM employees m JOIN employees e ON m.id = e.manager_id; joins employees as managers (m) to employees (e) where m.id = e.manager_id, listing managers.
  3. Step 3: Eliminate incorrect options

    B lists employees without managers, C has incorrect join condition, D is invalid syntax.
  4. Final Answer:

    SELECT DISTINCT m.name FROM employees m JOIN employees e ON m.id = e.manager_id; -> Option B
  5. Quick Check:

    Join on manager_id to find managers [OK]
Quick Trick: Join employees on manager_id to find managers [OK]
Common Mistakes:
  • Selecting employees without managers instead of managers
  • Using incorrect join conditions
  • Not using DISTINCT to avoid duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes