Completar frases Mastering SQL JoinsVersión en línea Drills to master SQL joins por Good Sam 1 ON departments.emp_id employees.name departments employees.id JOIN departments.dept_name employees FROM SELECT Problem 1 : Basic Inner Join Question : Given two tables , employees ( id , name ) and departments ( emp_id , dept_name ) , write a SQL query to find the names of employees and their corresponding department names . Solution : , = ; SELECT employees . name , departments . dept_name FROM employees JOIN departments ON employees . id = departments . emp_id ; Explanation : This query uses an inner join to fetch the employee name and department name where the employee's ID matches the employee ID in the departments table . 2 FROM employees.name LEFT JOIN ON employees.id = departments.emp_id departments employees departments.dept_name SELECT Problem 2 : Left Join Question : Display all employees and their department names , including those who do not belong to any department . Solution : , ; SELECT employees . name , departments . dept_name FROM employees LEFT JOIN departments ON employees . id = departments . emp_id ; Explanation : A left join ensures that all records from the employees table are included in the result set , even if there is no matching record in the departments table . 3 employees departments SELECT departments.dept_name FROM ON employees.id departments.emp_id RIGHT JOIN employees.name Problem 3 : Right Join Question : List all departments and any employees in them , including departments with no employees . Solution : , = ; SELECT employees . name , departments . dept_name FROM employees RIGHT JOIN departments ON employees . id = departments . emp_id ; Explanation : A right join ensures that all records from the departments table are shown , even if there is no matching employee . 4 FROM JOIN ON departments.dept_name OUTER employees SELECT FULL employees.id = departments.emp_id departments employees.name Problem 4 : Full Outer Join Question : Show a list of all employees and all departments , matched where possible . Solution : , ; SELECT employees . name , departments . dept_name FROM employees FULL OUTER JOIN departments ON employees . id = departments . emp_id ; Explanation : This query uses a full outer join to display all records from both tables , with matches where available and nulls where there is no match . 5 Manager Employee employees FROM e1 SELECT employees LEFT JOIN AS e1.manager_id = e2.id AS e1.name e2.name e2 ON Problem 5 : Self Join Question : For a table employees ( id , name , manager_id ) , list all employees and their manager's name . Solution : , ; SELECT e1 . name AS Employee , e2 . name AS Manager FROM employees e1 LEFT JOIN employees e2 ON e1 . manager_id = e2 . id ; Explanation : This self join uses the same table employees twice , with different aliases ( e1 for employees and e2 for managers ) , to link each employee with their respective manager based on the manager's ID .