Summary: in this tutorial, you will learn how to use the SQL Server RIGHT JOIN clause to query data from two tables.
Introduction to the SQL Server RIGHT JOIN clause #
The RIGHT JOIN is a clause of the SELECT statement. The RIGHT JOIN clause combines data from two or more tables.
The RIGHT JOIN clause starts selecting data from the right table and matching it with the rows from the left table. The RIGHT JOIN returns a result set that includes all rows in the right table, whether or not they have matching rows from the left table.
If a row in the right table does not have any matching rows from the left table, the column of the left table in the result set will have nulls.
The following shows the syntax of the RIGHT JOIN clause:
SELECT
select_list
FROM
T1
RIGHT JOIN T2 ON join_predicate;
Code language: SQL (Structured Query Language) (sql)In this syntax, T1 is the left table and T2 is the right table.
Note that RIGHT JOIN and RIGHT OUTER JOIN is the same. The OUTER keyword is optional.
The following Venn diagram illustrates the RIGHT JOIN operation:
SQL Server RIGHT JOIN example #
We will use the sales.order_items and production.products table from the sample database for the demonstration.