The RIGHT JOIN and the similar LEFT JOIN operators can be used in any FROM clause to combine records from two tables. The RIGHT JOIN operator is used to create a right outer join and includes all of the records from the second (right) of the two tables, even if there are no matching values for records in the first.
Although LEFT JOIN or RIGHT JOIN operators can be nested inside an INNER JOIN, the converse is not true. An INNER JOIN operator cannot be nested inside a LEFT JOIN or RIGHT JOIN.
SELECT Employee.Username, Project.ProjectName
FROM Employee RIGHT JOIN Project
ON Employee.EmployeeID = Project.EmployeeID;
Username ProjectName
Jack Smith Hardwork
Herman Allen Hardwork
Jill Swafford Hardwork
Bob Thornton Hardwork
Mike Sosebee Grindstone
Jill Swafford Grindstone
Zack Womble Hardwork
NULL Paperwork
NULL Dailytasks
(9 row(s) affected)
This example of a RIGHT JOIN returns the names of all projects, whether or not any of the employees were currently working on them, but with employee names where applicable.
The above example is in contrast to the INNER JOIN example which only returns those records in which the data in the joined fields is the same (i.e. only records for employees currently engaged in projects).