Right Join
RIGHT JOIN performs a join starting with the second (before join operation) table and then any matching first (after join operation) table records.
The result is NULL in the left side when there is no match.
SQL RIGHT JOIN Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
Lets use the “Orders” and “Products” tables to select all products with orders.
OrderID | ProductID | EmployeeID | OrderDate | CustomerID |
---|---|---|---|---|
10 | 2 | 1 | 9/9/2009 | 2 |
11 | 3 | 2 | 1/9/2009 | 3 |
12 | 4 | 1 | 3/9/2009 | 4 |
13 | 1 | 3 | 9/9/2010 | 5 |
14 | 3 | 1 | 9/30/2009 | 3 |
15 | 5 | 2 | 6/12/2009 | 1 |
16 | 3 | 4 | 4/9/2009 | 6 |
ProductID | ProductName | Description | Price | SupplierName | SupplierID |
---|---|---|---|---|---|
1 | Super Printer | 9000 series printer | 300 | TechOne | 1 |
2 | Laptop 5 | 4000 Laptop 5th generation | 700 | TechTwo | 2 |
3 | 3D Printer | 3D Print Pro | 900 | TechOne | 1 |
4 | Labtop 7 | 100 Labtop 7 processor | 1200 | TechTwo | 2 |
5 | Camera 400 | Camera Ultra 400 | 400 | TechThree | 3 |
SELECT
O.OrderID,
P.ProductID,
P.ProductName
FROM Orders as O
RIGHT JOIN Products as C
ON O.ProductID=P.ProductID
Results:
OrderID | ProductID | ProductName |
---|---|---|
13 | 1 | Super Printer |
10 | 2 | Laptop 5 |
11 | 3 | 3D Printer |
12 | 4 | Labtop 7 |
15 | 5 | Camera 400 |
SQL Tutorial