SQL RIGHT JOIN is a type of join that returns all the rows from the right table and the matching rows from the left table. If there is no match, it returns NULL values for the left table columns.
Syntax: The syntax for RIGHT JOIN is as follows:
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Let’s take a look at an example to understand the RIGHT JOIN better.
Example: Consider two tables, Employees and Departments. The Employees table has columns EmployeeID, EmployeeName, and DepartmentID. The Departments table has columns DepartmentID and DepartmentName.
To join the Employees and Departments tables using RIGHT JOIN, we would use the following SQL query:
SELECT Employees.EmployeeName, Departments.DepartmentName FROM Employees RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
The result of this query would be:
As you can see, all the rows from the Departments table are returned, along with the matching rows from the Employees table. In this case, Jane is the only employee who does not have a corresponding department, so the DepartmentName column is NULL for her row. Additionally, there is a row with NULL values for EmployeeName and DepartmentName because there is no matching row in either table.
Advanced examples about SQL Right Join
Here are some more advanced examples and additional notes to keep in mind when using RIGHT JOIN in SQL.
Using RIGHT JOIN to find unmatched rows
You can use RIGHT JOIN to find all the rows in the right table that do not have a matching row in the left table. For example, to find all the departments that do not have any employees, you can use the following query:
SELECT Departments.DepartmentName, Employees.EmployeeName FROM Departments RIGHT JOIN Employees ON Departments.DepartmentID = Employees.DepartmentID WHERE Employees.EmployeeID IS NULL;
This query returns all the departments that do not have any employees, along with the EmployeeName column that will be NULL for these rows.
Using multiple tables in a RIGHT JOIN
You can use multiple tables in a RIGHT JOIN to join three or more tables together. For example, if you have a third table called Orders with columns OrderID and EmployeeID, and you want to join it with the Employees and Departments tables, you can use the following query:
SELECT Employees.EmployeeName, Departments.DepartmentName, Orders.OrderID FROM Departments RIGHT JOIN Employees ON Departments.DepartmentID = Employees.DepartmentID RIGHT JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID;
This query returns all the rows from the Orders table, along with the matching rows from the Employees and Departments tables.
In a RIGHT JOIN, the order of the tables matters
The right table is the one that includes all the rows, while the left table only includes the matching rows. Therefore, the right table should always be the first table listed in the query.
Use caution when using RIGHT JOIN
Because RIGHT JOIN returns all the rows from the right table, it can be slower than other types of joins. Additionally, it can be difficult to read and understand the query results when the right table has a large number of rows.
Consider using LEFT JOIN instead
If you find that your RIGHT JOIN queries are becoming too complex or difficult to read, you may want to consider using a LEFT JOIN instead. A LEFT JOIN returns all the rows from the left table, along with the matching rows from the right table, and can often be easier to read and understand than a RIGHT JOIN.
In summary, a RIGHT JOIN returns all the rows from the right table and the matching rows from the left table. If there is no match, it returns NULL values for the left table columns. This type of join is useful when you want to include all the rows from a particular table, regardless of whether they have a matching row in the other table.