top of page

SQL Server Left And Right Join

A LEFT JOIN in SQL Server is a type of join operation that returns all rows from the left table (the table specified before the LEFT JOIN keyword), and the matched rows from the right table (the table specified after the LEFT JOIN keyword). If there is no match found in the right table, NULL values are returned for the columns of the right table.

Here’s the basic syntax of a LEFT JOIN in SQL Server:

SELECT columns
FROM left_table
LEFT JOIN right_table ON left_table.column = right_table.column;
 

Let’s illustrate this with an example. Suppose we have two tables, “Orders” and “Customers”. The “Orders” table contains information about orders, and the “Customers” table contains information about customers. We want to retrieve all orders along with the corresponding customer information, even if there are no matching customers for some number of characters in orders.

SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
 

In this following example:


We use a LEFT JOIN to join the “Orders” table (left table) with the “Customers” table (right table) based on the CustomerID column.


The query returns all rows from the “Orders” table, regardless of whether there is a matching customer in the “Customers” table.


If there is a matching customer for an order, the customer’s name is retrieved from the “Customers” table. If there is no matching customer, NULL is returned for the CustomerName column.

LEFT JOINs are useful when you want to include all rows from the left table in the result set, regardless of whether there are matching rows in the syntax left or right table. They are commonly used for situations where you want to retrieve data from one table along with related data from another table, and you want to include all rows from the first table, even if there are no matches in the second table.

What is the difference between an Inner Join and a Left Join

The main difference between an INNER JOIN and a LEFT JOIN lies in how they handle unmatched rows between the tables being joined:


Inner Join:


An INNER JOIN returns only the rows that have matching values in both tables based on the specified join condition.


If there are no matching rows between the tables, those rows are excluded from the result set.


In other words, an INNER JOIN only returns rows where there is a match between the columns being joined.


Left Join:


A LEFT JOIN returns all rows from the left table (the table specified before the LEFT JOIN keyword), along with the matching rows from the right table (the table specified after the LEFT JOIN keyword).


If there are no matching rows in the right table, NULL values are returned for the columns of the right table in the result set.


In other words, a LEFT JOIN ensures that all rows from the left table are included in the result set, even if there are no matching rows in the right table.

In summary:


An INNER JOIN returns only the matching rows between the tables based on the join condition.


A LEFT JOIN returns all rows from the left table and the matching rows from the right table, with NULL values for columns from the right table if there is no match.

Here’s a visual representation to illustrate the difference:

Inner Join:

Table A          Table B
+-------+       +-------+
| Col1  |       | Col2  |
+-------+       +-------+
|   1   |       |   1   |
|   2   |       |   3   |
|   3   |       |   5   |
+-------+       +-------+

Result of INNER JOIN on Col1 = Col2:
+-------+-------+
| Col1  | Col2  |
+-------+-------+
|   1   |   1   |
+-------+-------+


Left Join:

Table A          Table B
+-------+       +-------+
| Col1  |       | Col2  |
+-------+       +-------+
|   1   |       |   1   |
|   2   |       |   3   |
|   3   |       |   5   |
+-------+       +-------+

Result of LEFT JOIN on Col1 = Col2:
+-------+-------+
| Col1  | Col2  |
+-------+-------+
|   1   |   1   |
|   2   |  NULL |
|   3   |  NULL |
+-------+-------+
 

In the inner join result, only the row with a matching value in both tables is returned. In the left side of join result, all rows from Table A are returned, with NULL values for non-matching rows from Table B.

When Should I use a Left Join and a Right Joins

The decision to use a LEFT JOIN or a RIGHT JOIN depends on the specific requirements of data type in your query and the relationship between the tables involved. Here are some guidelines to help you decide when to use each type of join:

Use LEFT JOIN when:


You want to retrieve all rows from the left table (the table specified before the LEFT JOIN keyword), even if there are no matching rows in the right table.


You need to include all records from the left table and only the matching records from the right table.


You are working with a parent-child relationship, where the left table represents the parent entity and you want to retrieve related child entities along with any parent entities that do not have related child entities.


You want to perform operations such as filtering, grouping, or aggregating based on the columns from the left table.

Use RIGHT JOIN when:


You want to retrieve all rows from the right table (the table specified after the RIGHT JOIN keyword), even if there are no matching rows in the left table.


You need to include all records from the right table and only the matching records from the left table.


You are working with a child-parent relationship, where the right table represents the child entity and you want to retrieve related parent entities along with any child entities that do not have related parent entities.


You want to perform operations such as filtering, grouping, or aggregating based on the columns from the right table.

General considerations:


If you’re unsure which join to use, it’s often a good idea to visualize the relationship between the tables and think about which table’s data is more important or central to your query.


Consider the cardinality of the relationship between the tables. If one table has many rows matching a single row in the other table, it might make sense to use a LEFT JOIN or RIGHT JOIN accordingly.


If possible, review sample data and run test queries with different types of joins to verify that the results meet your expectations.

Ultimately, the choice between a LEFT JOIN and a RIGHT JOIN depends on your specific data model and the requirements of your query. Understanding the differences between the two types of joins and their implications will help you make an informed decision.

What is the difference between left and Right Joins

The main difference between a LEFT JOIN and a RIGHT JOIN lies in the treatment of specified number of unmatched rows between the tables being joined:


Left Join:


A LEFT JOIN returns all rows from the left table (the table specified before the LEFT JOIN keyword), along with the matching rows from the right table (the table specified after the LEFT JOIN keyword).


If there are no matching rows in the right table, NULL values are returned for the columns of the right table in the result set.


In other words, a LEFT JOIN ensures that all rows from the left table are included in the result set, even if there are no matching rows in the right table.


Right Join:


A RIGHT JOIN returns all rows from the right table, along with the matching rows from the left table.


If there are no matching rows in the left table, NULL values are returned for the columns of the left table in the result set.


In other words, a RIGHT JOIN ensures that all rows from the right table are included in the result set, even if there are no matching rows in the left table.

In summary:


A LEFT JOIN returns all rows from the left table and the matching rows from the right table, with NULL values for columns from the right table if there is no match.


A RIGHT JOIN returns all rows from the right table and the matching rows from the left table, with NULL values for columns from the left table if there is no match.

Here’s a visual representation to illustrate the difference:

Left Join:

Table A          Table B
+-------+       +-------+
| Col1  |       | Col2  |
+-------+       +-------+
|   1   |       |   1   |
|   2   |       |   3   |
|   3   |       |   5   |
+-------+       +-------+

Result of LEFT JOIN on Col1 = Col2:
+-------+-------+
| Col1  | Col2  |
+-------+-------+
|   1   |   1   |
|   2   |  NULL |
|   3   |  NULL |
+-------+-------+


Right Join:

Table A          Table B
+-------+       +-------+
| Col1  |       | Col2  |
+-------+       +-------+
|   1   |       |   1   |
|   2   |       |   3   |
|   3   |       |   5   |
+-------+       +-------+

Result of RIGHT JOIN on Col1 = Col2:
+-------+-------+
| Col1  | Col2  |
+-------+-------+
|   1   |   1   |
|  NULL |   3   |
|  NULL |   5   |
+-------+-------+
 

In the left join result, all rows from Table A are returned, with NULL values for non-matching rows from Table B. In the right join result, all rows from Table B are returned, with NULL values for non-matching rows from Table A.

Let’s review an example where we have two tables, “Orders” and “Customers”. We want to retrieve all orders along with the corresponding customer information, but we want to match orders based on both the CustomerID and Country columns. We’ll use a LEFT JOIN with multiple conditions in the ON clause.

Here’s how you can do it:

SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID AND Orders.Country = Customers.Country;
 

In this example:


We use a LEFT JOIN to join the “Orders” table (left table) with the “Customers” table (right table).


We specify multiple conditions in the ON clause:


Orders.CustomerID = Customers.CustomerID: This condition ensures that we match orders with customers based on their CustomerID.


Orders.Country = Customers.Country: This condition ensures that we further filter the matching based on the country of the customer.


The query returns all rows from the “Orders” table, regardless of whether there are matching customers in the “Customers” table.


If there is a matching customer for an order based on both CustomerID and Country, the customer’s name is retrieved from the “Customers” table. If there is no matching customer, NULL is returned for the CustomerName column.

This example demonstrates how you can use a LEFT JOIN with multiple conditions in the ON clause to retrieve data from two tables based on multiple criteria. It’s useful when you need to select left join tables based on complex conditions involving multiple columns.

Examples of Both Left and Right Joins

Let’s use two tables, “Orders” and “Customers”, to illustrate examples of both LEFT JOIN and RIGHT JOIN.

LEFT JOIN Example: Suppose we have two tables, “Orders” and “Customers”, where “Orders” contains information about orders and “Customers” contains information about customers. We want to retrieve all orders along with the corresponding customer information, even if there are no matching customers for some orders.

sql 
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
 

In this example:


We use a LEFT JOIN to join the “Orders” table (left table) with the “Customers” table (right table) based on the CustomerID column.


The query returns all rows from the “Orders” table, regardless of whether there are matching customers in the “Customers” table.


If there is a matching customer for an order, the customer’s name is retrieved from the “Customers” table. If there is no matching customer, NULL is returned for the CustomerName column.

RIGHT JOIN Example: Suppose we want to retrieve all customers along with their corresponding orders, even if there are no matching orders left part for some customers.

SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderDate
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
 

In this example:


We use a RIGHT JOIN to join the “Customers” table (left table) with the “Orders” table (right table) based on the CustomerID column.


The query returns all rows from the “Customers” table, regardless of whether there are matching orders in the “Orders” table.


If there is a matching order for a customer, the order ID and order date are retrieved from the “Orders” table. If there is no matching order, NULL is returned for the OrderID and OrderDate columns.

These examples demonstrate how LEFT JOIN and RIGHT JOIN differ in their treatment of specified number of characters in unmatched rows between the tables being joined.

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Get in Touch

Thanks for submitting!

bottom of page