Bird
0
0

Given the employees table with id, name, and manager_id, how can you write a query to find all employees who are managers themselves (i.e., have at least one employee reporting to them)?

hard📝 Application Q9 of 15
SQL - Advanced Joins
Given the employees table with id, name, and manager_id, how can you write a query to find all employees who are managers themselves (i.e., have at least one employee reporting to them)?
ASELECT DISTINCT m.name FROM employees e JOIN employees m ON e.manager_id = m.id;
BSELECT name FROM employees WHERE manager_id IS NULL;
CSELECT name FROM employees WHERE id NOT IN (SELECT manager_id FROM employees);
DSELECT name FROM employees WHERE manager_id = id;
Step-by-Step Solution
Solution:
  1. Step 1: Identify managers

    Managers are employees whose id appears as manager_id for others.
  2. Step 2: Use self join to find managers

    Join employees as e to managers as m where e.manager_id = m.id, then select distinct manager names.
  3. Final Answer:

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

    Managers have employees reporting to them [OK]
Quick Trick: Join employees on manager_id to find managers [OK]
Common Mistakes:
MISTAKES
  • Selecting employees with NULL manager_id
  • Using wrong subqueries
  • Confusing manager and employee roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes