ADVERTISEMENT

SQL RIGHT JOIN

RIGHT JOIN returns all rows from the right table, and matched rows from the left. If no match exists, left-side columns return NULL.

🔹 Basic Syntax

SELECT columns
FROM table1
RIGHT JOIN table2
  ON table1.common_column = table2.common_column;

🔹 Example: List All Departments and Their Employees

SELECT
  e.employee_name,
  d.department_name
FROM employees e
RIGHT JOIN departments d
  ON e.department_id = d.department_id;

✅ All departments are shown — even those with no employees (in that case, employee_name is NULL).

🔹 Use Case: Find Departments Without Employees

SELECT d.department_name
FROM employees e
RIGHT JOIN departments d
  ON e.department_id = d.department_id
WHERE e.employee_id IS NULL;

🔍 This identifies departments that currently have no employees.

🧠 Quick Recap

Key PointExplanation
RIGHT JOINKeeps all records from right table
No match?Left table columns return NULL
Useful forHighlighting unmatched records in the right table
Similar toLEFT JOIN, but direction is reversed

➡️ Use RIGHT JOIN when right table data must be fully preserved

ADVERTISEMENT