SQL · Pattern 6 — Joins
RIGHT JOIN
RIGHT JOIN returns all rows from the right table and matching rows from the left table.
Source data — Pattern 6: Joins
All queries run against the shared employees and departments tables (see schema.sql). This pattern introduces the departments table. Topic 38 also needs a projects table, while topic 39 introduces a salary_grades table for non-equi joins.




What it does
RIGHT JOIN returns all rows from the right table and matching rows from the left table. If no employee belongs to a department, employee columns return NULL.
Problem
Display all departments, including those without employees.
SELECT e.first_name,
d.department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id;
Result
