🔹 RIGHT JOIN in SQL

Definition:
The RIGHT JOIN clause returns all records from the right table, and the matched records from the left table. If there is no match, the result is NULL from the left table.
Syntax:
SELECT table1.column, table2.column
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT Employees.name, Departments.department_name
FROM Employees
RIGHT JOIN Departments
ON Employees.department_id = Departments.department_id;
Sample Employees Table:
idnamedepartment_id
1Alice101
2Bob102
3Charlie103
Sample Departments Table:
department_iddepartment_name
101HR
102Engineering
104Finance
Output:
namedepartment_name
AliceHR
BobEngineering
NULLFinance
Explanation:
The query returns all rows from the Departments table: - HR (101) → Alice - Engineering (102) → Bob - Finance (104) → No match in Employees → **NULL**

Charlie (dept 103) has no matching department → **not shown** RIGHT JOIN ensures every row from the right table appears, matched or not.
Usage Tips:
  • Use RIGHT JOIN when you want all rows from the second (right) table regardless of matches.
  • Useful when the right table is primary, and you want to know which rows aren't linked in the left table.
Common Mistake:
Assuming RIGHT JOIN excludes unmatched right table rows. It doesn't — unmatched right table rows are included, with NULLs for left table columns.
📘 Note: RIGHT JOIN is sometimes called RIGHT OUTER JOIN; both are the same.
➡️ Next: FULL JOIN