Right Join

The RIGHT JOIN combines data from two or more tables. The RIGHT JOIN starts selecting data from the right table and matching 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 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.
Syntax
SELECT 
    select_list
FROM 
    T1
RIGHT JOIN T2 ON join_predicate;

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 illustration

products order_items

SELECT
    product_name,
    order_id
FROM
    sales.order_items o
    RIGHT JOIN production.products p 
        ON o.product_id = p.product_id
ORDER BY
    order_id;

No comments:

Post a Comment