Menu

SQL Left Join – A Comprehensive Guide on SQL Left Join

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

In the realm of SQL, or Structured Query Language, we often come across situations where data is spread across different tables, and combining them becomes necessary. One of the vital methods to combine data from two or more tables is the SQL Left Join.

SQL Left Join Syntax

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

Here’s how the syntax looks:

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

This command will result in a new table that includes rows from the left table and the matched rows from the right one.

Unpacking the Syntax with Examples

To make this more digestible, let’s look at an example. We’ll continue using the Orders and Customers tables.

Here’s a refresher on what the Orders table looks like:

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

And here’s the Customers table:

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

Our goal is to create a table that includes all customers from the Customers table, and wherever possible, their corresponding order from the Orders table.

Here’s how you use the Left Join syntax to achieve this:

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

Executing this command gives us a new table:

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

The result contains all the customers, whether they have made an order or not. If a customer hasn’t made an order, the OrderID and Product fields will be NULL.

Wrapping Up

Mastering the use of Left Join in SQL is a major step in becoming proficient in handling and analyzing relational data. It lets you compile and relate data from multiple tables based on shared attributes while ensuring that no data from the left table is lost.

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