Complete the code to select all employees and their managers using a self join.
SELECT e.name AS employee, m.name AS manager FROM employees e JOIN employees m ON e.manager_id = [1].id;The alias m refers to the managers table (which is the same employees table but aliased). We join on e.manager_id = m.id to link employees to their managers.
Complete the code to find employees whose manager's name is 'Alice'.
SELECT e.name FROM employees e JOIN employees m ON e.manager_id = m.id WHERE m.name = [1];We want to find employees whose manager's name is 'Alice'. So the condition is m.name = 'Alice'.
Fix the error in the code to correctly list employees and their managers.
SELECT e.name, m.name FROM employees e JOIN employees m ON e.manager_id = [1].id;The join condition should match e.manager_id = m.id, not m.manager_id. So the alias before id is m.
Fill both blanks to select employees and their managers who work in the same department.
SELECT e.name, m.name FROM employees e JOIN employees m ON e.manager_id = m.id WHERE e.[1] = m.[2];
To find employees and managers in the same department, compare e.department_id with m.department_id.
Fill all three blanks to create a query that lists employees, their managers, and the manager's department name.
SELECT e.name AS employee, m.name AS manager, d.[1] FROM employees e JOIN employees m ON e.[2] = m.[3] JOIN departments d ON m.department_id = d.id;
The query selects the department name from the departments table. The join between employees and managers is on e.manager_id = m.id.