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 πΉ Example: List All Departments and Their Employees β All departments are shown β even those with no employees (in that case, employee_name is NULL). πΉ Use Case: Find […]
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 Point | Explanation |
|---|---|
RIGHT JOIN | Keeps all records from right table |
| No match? | Left table columns return NULL |
| Useful for | Highlighting unmatched records in the right table |
| Similar to | LEFT JOIN, but direction is reversed |
β‘οΈ Use RIGHT JOIN when right table data must be fully preserved
Was this helpful?
Thanks for your feedback!