Understanding Advanced SQL Joins with Online Shop's Orders Data
Introduction to Diving Deeper into Joins
Great work! You've grasped the essentials and have now explored INNER JOIN in the context of an online shopping platform. Now it's time to delve deeper into SQL JOINs using data related to customer orders and order items. We will focus on LEFT JOIN and RIGHT JOIN. Before we start working with the more detailed order data at our disposal, it's vital for us to clearly understand these joins through a simpler example.
Recap of SQL Joins
Before we start, keep in mind that SQL JOINs allow us to combine data from two or more tables based on a related column. Previously, we've worked with INNER JOIN, which selects rows that have matching values in both tables. In this lesson, we'll explore how LEFT JOIN and RIGHT JOIN can help us manipulate our data further.
Sample Tables Overview
To understand these JOIN types better, consider two simple tables: Orders and OrderItems.
Orders Table:
| order_id | customer_id | order_date | order_status |
|---|---|---|---|
| 1 | 41 | 2021-08-17 | Delivered |
| 2 | 16 | 2022-04-03 | Processed |
The Orders table provides detailed information about each order, including the order_id, date, status, and the customer who placed the order. This is useful for tracking order specifics.
OrderItems Table:
| order_item_id | order_id | product_id | extended_support |
|---|---|---|---|
| 1 | 1 | 25 | 0 |
| 2 | 2 | 12 | 0 |
The OrderItems table connects each order to the specific items within it using corresponding order_id values.
INNER JOIN Explained
INNER JOIN returns rows when there's a match in both tables. If there's no match, those rows are not included in the output.
Example:
