Complete the code to select employee names and their managers' names using a self join.
SELECT e.name AS employee, m.name AS manager FROM employees e JOIN employees m ON e.manager_id = [1];The self join matches each employee's manager_id with the manager's id. So, e.manager_id = m.id links employees to their managers.
Complete the code to find pairs of employees who share the same manager.
SELECT e1.name AS employee1, e2.name AS employee2 FROM employees e1 JOIN employees e2 ON e1.[1] = e2.[1] WHERE e1.id <> e2.id;
Employees who share the same manager have the same manager_id. The join condition matches e1.manager_id = e2.manager_id.
Fix the error in the self join to correctly list employees and their managers.
SELECT e.name, m.name FROM employees e JOIN employees m ON e.[1] = m.[2];
The join should match the employee's manager_id to the manager's id. So, e.manager_id = m.id is correct.
Fill both blanks to select employees and their managers' departments using a self join.
SELECT e.name, m.name, m.[1] FROM employees e JOIN employees m ON e.[2] = m.id;
The query selects the manager's department by joining on e.manager_id = m.id.
Fill all three blanks to find employees who manage others and count how many they manage.
SELECT m.name AS manager, COUNT(e.[1]) AS team_size FROM employees e JOIN employees m ON e.[2] = m.[3] GROUP BY m.name HAVING COUNT(e.id) > 0;
The count is on employee id. The join matches e.manager_id = m.id to find employees managed by each manager.