SQL
Efficiently Join Multiple Tables with INNER JOIN
Learn to combine data from two or more tables using INNER JOIN to retrieve related records, essential for relational database queries and reporting.
SELECT
o.order_id,
c.customer_name,
p.product_name,
oi.quantity,
oi.total_price
FROM
Orders AS o
JOIN
Customers AS c ON o.customer_id = c.customer_id
JOIN
OrderItems AS oi ON o.order_id = oi.order_id
JOIN
Products AS p ON oi.product_id = p.product_id
WHERE
o.order_date >= '2023-01-01';
How it works: This snippet demonstrates how to link records across multiple related tables using `INNER JOIN`. It retrieves order details, customer names, product names, and quantities by matching common columns (`customer_id`, `order_id`, `product_id`) between the `Orders`, `Customers`, `OrderItems`, and `Products` tables. A `WHERE` clause is included to filter for orders placed after a specific date, showcasing practical data retrieval from a normalized schema.