RIGHT JOIN preserves all rows from the right table and matching rows from the left table. Filtering conditions determine which records remain in the final result.
Understanding filter placement helps prevent accidentally removing unmatched records from an outer join.
Return all departments and employees earning more than ₹50,000 while preserving departments without qualifying employees.
SELECT
d.DepartmentName,
e.EmployeeName,
e.Salary
FROM Employees e
RIGHT JOIN Departments d
ON e.DepartmentID = d.DepartmentID
AND e.Salary > 50000;
RIGHT JOIN filtering can be useful when the right-side table represents the primary business population.
- RIGHT JOIN preserves rows from the right table.
- ON conditions control matching.
- WHERE conditions can remove unmatched rows.
- RIGHT JOIN can often be rewritten as LEFT JOIN by changing table order.
- Understand which table must be preserved.
Which table is preserved by RIGHT JOIN?
RIGHT JOIN preserves all rows from the right-side table.
Can RIGHT JOIN be rewritten as LEFT JOIN?
Yes. The tables can usually be reversed and a LEFT JOIN used instead.