## Introduction

Welcome to Lesson 2.3: Joining Tables. This lesson is part of Module 2: Core Skills - Data Manipulation Techniques in the Data Analytics for Business Leaders course. In this lesson, we'll explore the essential concepts of INNER JOIN, LEFT JOIN, and RIGHT JOIN, along with practical examples of how to combine data from multiple tables and their use cases in the finance and fintech sectors. By the end of this lesson, you will be able to understand how to effectively join tables to enhance your data analysis capabilities, leading to more insightful business decisions.

## Main Content

### INNER JOIN

The INNER JOIN is one of the most commonly used methods for combining tables in a database. It allows you to retrieve records that have matching values in both tables. Imagine you have two tables: one containing customer information and another containing their order details. Using an INNER JOIN, you can create a new dataset that includes only those customers who have placed orders.

For example, consider the following tables:

**Customers Table:**
| CustomerID | Name        |
|------------|-------------|
| 1          | John Smith  |
| 2          | Jane Doe    |
| 3          | Mike Johnson |

**Orders Table:**
| OrderID | CustomerID | OrderAmount |
|---------|------------|-------------|
| 101     | 1          | 250         |
| 102     | 3          | 150         |
| 103     | 4          | 300         |

Using an INNER JOIN on `CustomerID`, you would retrieve the following results:

| CustomerID | Name        | OrderID | OrderAmount |
|------------|-------------|---------|-------------|
| 1          | John Smith  | 101     | 250         |
| 3          | Mike Johnson | 102     | 150         |

This highlights how INNER JOIN provides a focused view by filtering out customers without orders.

### LEFT JOIN

Next, we have the LEFT JOIN, which is particularly useful when you want to retrieve all records from the left table and the matched records from the right table. If there is no match, the result will contain NULL values for the right table's columns.

Using the same example, if you perform a LEFT JOIN on the Customers table and Orders table, you will get all customers, regardless of whether they have placed an order or not:

```sql
SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.OrderAmount
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```

The resulting dataset would look like this:

| CustomerID | Name        | OrderID | OrderAmount |
|------------|-------------|---------|-------------|
| 1          | John Smith  | 101     | 250         |
| 2          | Jane Doe    | NULL    | NULL        |
| 3          | Mike Johnson | 102     | 150         |

Here, Jane Doe appears in the results with NULL values for `OrderID` and `OrderAmount`, indicating she has not made any orders.

### RIGHT JOIN

The RIGHT JOIN operates similarly to the LEFT JOIN, but it returns all records from the right table and the matched records from the left table. If there is no match, the result will contain NULL values for the left table's columns.

Using the previous example, if we perform a RIGHT JOIN on the Orders table, we would retrieve all orders, including those that do not correspond to any customer:

```sql
SELECT Customers.CustomerID, Customers.Name, Orders.OrderID, Orders.OrderAmount
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```

The resulting dataset would look like this:

| CustomerID | Name        | OrderID | OrderAmount |
|------------|-------------|---------|-------------|
| 1          | John Smith  | 101     | 250         |
| 3          | Mike Johnson | 102     | 150         |
| NULL       | NULL        | 103     | 300         |

In this case, the last row shows an order that was placed by a non-existent customer (CustomerID 4), which illustrates how RIGHT JOIN can help identify discrepancies in your data.

## Examples

In the context of finance and fintech, joining tables can provide invaluable insights. Let's explore two practical examples:

1. **Customer Segmentation and Spending Behavior**: In a scenario where you have a table of customer demographics and another containing transaction histories, you can use INNER JOIN to analyze spending behaviors based on specific customer segments. For instance, if you're looking to understand how different age groups spend their money, joining these tables can help uncover patterns that inform marketing strategies.

2. **Risk Assessment**: In risk management, you may have a table listing loans and another detailing borrower credit scores. By using LEFT JOIN, you can identify which loans do not have associated credit scores, allowing you to assess potential risks in lending practices.

## Conclusion

In this lesson, we covered the fundamental concepts of joining tables, including INNER JOIN, LEFT JOIN, and RIGHT JOIN. We explored how each type of join works and provided practical examples relevant to the finance and fintech industries. Understanding how to effectively join tables enhances your data analysis capabilities, enabling you to derive more meaningful insights from your data. In the next lesson, we will delve into data aggregation techniques that complement our understanding of data manipulation.

## Reflection Questions
1. How can you apply INNER JOIN in your current role to analyze customer data more effectively?
2. In what scenario might you prefer using a LEFT JOIN over an INNER JOIN in your analysis?
3. Reflect on your understanding of table joins: What insights do you believe they can provide in your professional context? 

**Word Count: 1,024**