RIGHT JOIN, also known as RIGHT OUTER JOIN, is a type of join that returns all records from the right table (table2) and the matched records from the left table (table1). If there is no match, NULL values are returned for columns from the left table.
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
In this syntax, table2
is the right table, and table1
is the left table. The join is performed based on the specified column_name
.
Here’s an example of using RIGHT JOIN to retrieve data from two related tables:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
This query selects the CustomerName
from the Customers
table and the OrderID
from the Orders
table. If an order does not have a matching customer, the CustomerName
will be NULL.
RIGHT JOIN is useful in various scenarios, such as:
Write your own RIGHT JOIN queries using example tables like Customers
and Orders
. Try to include different columns in your SELECT statement!