Menu

SQL Right Join – A Comprehensive Guide on SQL right Join

Right Join in SQL is a method to combine rows from two or more tables based on a related column

In Structured Query Language, or SQL, we frequently need to combine data distributed across multiple tables. Among the various methods to achieve this, one crucial tool in our toolbox is the SQL Right Join.

SQL Right Join Syntax

A Right Join in SQL is a type of join that returns all the rows from the right table and the matched rows from the left table. If there is no match, the result is NULL on the left side.

Let’s examine its syntax:

SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Executing this command will generate a new table that includes rows from the right table and any matching rows from the left table.

Unpacking the Syntax with Examples

To further clarify this, let’s delve into an example using the Orders and Customers tables.

Let’s remind ourselves of the Orders table:

| OrderID | CustomerID | Product    |
|---------|------------|------------|
| 1       | 101        | Apples     |
| 2       | 102        | Bananas    |
| 3       | 103        | Cherries   |
| 4       | 104        | Dates      |
| 5       | 105        | Eucalyptus |

And the Customers table:

| CustomerID | Name     | Country  |
|------------|----------|----------|
| 101        | Alice    | USA      |
| 102        | Bob      | UK       |
| 103        | Charlie  | Canada   |
| 104        | David    | Australia|

Our objective is to create a table that includes all orders from the Orders table, and wherever possible, the corresponding customer from the Customers table.

Here’s how I’d use the Right Join syntax to achieve this:

SELECT Orders.OrderID, Customers.Name, Orders.Product
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

Executing this command gives us a new table:

| OrderID | Name     | Product   |
|---------|----------|-----------|
| 1       | Alice    | Apples    |
| 2       | Bob      | Bananas   |
| 3       | Charlie  | Cherries  |
| 4       | David    | Dates     |
| 5       | NULL     | Eucalyptus|

This result includes all orders, whether they have an associated customer or not. If an order does not have a matching customer, the Name field will be NULL.

Wrapping Up

Getting a good grip on Right Join operations in SQL is an important step in managing and analyzing relational data effectively. It allows you to assemble and link data from multiple tables based on common attributes, while ensuring that no data from the right table is missed.

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science