Complete the code to select all columns from the left table and join with the right table.
SELECT * FROM employees [1] departments ON employees.department_id = departments.id;The LEFT JOIN keyword returns all rows from the left table (employees), and the matched rows from the right table (departments). If there is no match, the result is NULL on the right side.
Complete the code to select employee names and their department names, preserving all employees even if they have no department.
SELECT employees.name, [1].name AS department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;We want the department name from the departments table, so we select departments.name.
Fix the error in the join condition to correctly join employees with departments preserving all employees.
SELECT employees.name, departments.name FROM employees LEFT JOIN departments ON employees.[1] = departments.id;The join condition must match employees.department_id with departments.id to link employees to their departments.
Fill both blanks to select employee names and their department names, showing 'No Department' if the department is missing.
SELECT employees.name, COALESCE(departments.[1], [2]) AS department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;
We use departments.name to get the department name. If it is NULL, COALESCE returns the string 'No Department' instead.
Fill all three blanks to count employees per department, including departments with zero employees.
SELECT departments.[1], COUNT(employees.[2]) AS employee_count FROM departments LEFT JOIN employees ON employees.[3] = departments.id GROUP BY departments.name;
We select departments.name to show the department name. We count employees.id to count employees. The join condition matches employees.department_id to departments.id.