joining 3 table in sql with example

Joining Three Tables in SQL: A Comprehensive Example

By Michael Smith
AD

In the realm of relational databases, querying data often requires combining information from multiple tables. This is where the SQL JOIN clause comes into play, enabling you to combine rows from two or more tables, based on a related column between them. Let's delve into the world of SQL JOINs, using three tables as an example.

Consider three tables: 'Customers', 'Orders', and 'OrderItems', with the following simplified structures:

  • Customers: CustomerID (PK), CustomerName, ContactName, Country
  • Orders: OrderID (PK), CustomerID (FK), OrderDate, ShipperID
  • OrderItems: OrderID (FK), ProductID (FK), Quantity, Price

Each of these tables is related to the others, with foreign key relationships indicated by the (FK) abbreviation. Now, let's explore SQL JOIN operations with these tables.

AD
SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
the table is labeled with numbers and symbols for each type of item in this diagram
the table is labeled with numbers and symbols for each type of item in this diagram
SQL Joins Explained with Examples | Inner Join, Left Join, Right Join & Full Join
SQL Joins Explained with Examples | Inner Join, Left Join, Right Join & Full Join

JOIN Operations

SQL offers several types of JOIN operations – INNER JOIN, LEFT (OUTER) JOIN, RIGHT (OUTER) JOIN, and FULL (OUTER) JOIN. We'll inspect these, along with the CROSS JOIN and SELF JOIN, using our three tables.

INNER JOIN

The INNER JOIN returns records that have matching values in both tables. To retrieve order details along with customer information, use: ```sql SELECT Customers.CustomerName, Orders.OrderID, OrderItems.ProductID, OrderItems.Quantity, OrderItems.Price FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID; ```

How to join three tables in SQL query – MySQL Example
How to join three tables in SQL query – MySQL Example
an info sheet with several different types of letters and numbers in blue, white, and green
an info sheet with several different types of letters and numbers in blue, white, and green
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners

This query combines all three tables, showing the customer name, order ID, product ID, quantity, and price for each order item.

LEFT (OUTER) JOIN

A LEFT JOIN returns all records from the left table (Customers), and the matched records from the right table (Orders). If there's no match, the result is NULL on the right side. ```sql SELECT Customers.CustomerName, Orders.OrderID, OrderItems.ProductID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID LEFT JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID; ```

AD

In this example, even if a customer has no orders, their information will appear in the result set with NULL values for OrderID and other related fields.

MySQL for Beginners -Joining Tables.(Part-11)
MySQL for Beginners -Joining Tables.(Part-11)
a poster with the names and symbols for different types of letters
a poster with the names and symbols for different types of letters
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL Joins
SQL Joins
How to Join Two Unrelated Tables in SQL: A Must-Know Interview Question
How to Join Two Unrelated Tables in SQL: A Must-Know Interview Question
How to Learn SQL JOINs
How to Learn SQL JOINs

Explore more about RIGHT JOIN, FULL JOIN, CROSS JOIN, and SELF JOIN in a similar manner, applying the same structure for explanations and examples.

Multiple JOINs

Sometimes, you need to combine data from more than two tables. No problem – SQL allows multiple JOINs in a single query.

AD

Let's say you want to include the shipper's name in our previous INNER JOIN example. We'll need a 'Shippers' table with ShipperID and CompanyName fields. Here's how to achieve this: ```sql SELECT Customers.CustomerName, Orders.OrderID, OrderItems.ProductID, OrderItems.Quantity, OrderItems.Price, Shippers.CompanyName FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID; ```

the four basic tables for each table
the four basic tables for each table
three vennuous circles with the words right table, left table and right table
three vennuous circles with the words right table, left table and right table
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
What is a join in SQL?
What is a join in SQL?
What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?
SQL cross join Example and it's performance
SQL cross join Example and it's performance
Inner Join - Animated
Inner Join - Animated
SQL SELF JOIN Explained with Examples | Beginner SQL Tutorial
SQL SELF JOIN Explained with Examples | Beginner SQL Tutorial
Diferencia entre Join e Inner join SQL | Ejercicios de excel
Diferencia entre Join e Inner join SQL | Ejercicios de excel
SQL Joins Explained | INNER vs LEFT vs RIGHT JOIN Cheat Sheet
SQL Joins Explained | INNER vs LEFT vs RIGHT JOIN Cheat Sheet
an image of a diagram with the names and symbols for soljoins
an image of a diagram with the names and symbols for soljoins
What is a Table in SQL? (Beginner Guide)
What is a Table in SQL? (Beginner Guide)

In this query, we've added one more INNER JOIN to include the Shipper's name in our results.

Now that you've seen how to perform JOIN operations using three tables, you're equipped to tackle more complex queries. Practice with different join types, explore other SQL functions, and don't hesitate to delve into your database's documentation for further insights. Happy coding!

AD
AD