Master SQL Join with 3 Tables: Optimize Your Queries Now

When working with relational databases, it's often necessary to combine data from multiple tables. SQL provides the JOIN operation to link tables together based on a related column between them. Here, we'll discuss how to perform a JOIN operation with three tables in SQL.

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

Before we dive into the three-table JOIN, let's refresh our understanding of the JOIN concept with a simple two-table example. Consider two tables, 'Orders' and 'Customers', with a common column 'CustomerID'.

Top 50 Microsoft SQL Server Interview Questions Answers for 1 to 3 Years Experienced
Top 50 Microsoft SQL Server Interview Questions Answers for 1 to 3 Years Experienced

Understanding SQL JOINs

In this example, we'll use an INNER JOIN to combine rows from both tables where there's a match in 'CustomerID'.

Give me 2 mins—I'll teach you how SQL JOINs work: The Join clause lets… | Neo Kim | 82 comments
Give me 2 mins—I'll teach you how SQL JOINs work: The Join clause lets… | Neo Kim | 82 comments

```sql SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID; ```

The result will be a table with 'OrderID' and 'CustomerName' columns, showing orders along with the customer names.

Types of JOINs

SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide

There are several types of JOINs in SQL: INNER JOIN, LEFT (OUTER) JOIN, RIGHT (OUTER) JOIN, and FULL (OUTER) JOIN. Familiarize yourself with each to choose the right one for your use case.

Joining Three Tables

Now, let's consider three tables: 'Orders', 'Customers', and 'Employees'. The 'Employees' table has a 'ManagerID' field that references 'CustomerID' in the 'Customers' table. We want to see orders along with the customer's name and their manager's name.

How to Join 3 tables in 1 SQL query
How to Join 3 tables in 1 SQL query

Three-Table JOIN Example

To achieve this, we'll perform a three-table JOIN using INNER JOINs.

```sql SELECT Orders.OrderID, Customers.CustomerName, Employees.EmployeeName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN Employees ON Customers.ManagerID = Employees.EmployeeID; ```

The result will now include the 'OrderID', 'CustomerName', and 'EmployeeName' (manager's name) columns.

SQL Server | Simplest Way to Learn INNER JOIN Between 3 Tables 💻 Step by Step
SQL Server | Simplest Way to Learn INNER JOIN Between 3 Tables 💻 Step by Step

Using LEFT JOINs in Three-Table Queries

Sometimes, you might want to include all records from one table and matching records from other tables. In such cases, use a LEFT JOIN. Here's how you can modify the previous query to include all customers, even if they don't have a related order or manager:

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
MySQL for Beginners -Joining Tables.(Part-11)
MySQL for Beginners -Joining Tables.(Part-11)
Joining Three or More Tables in SQL - GeeksforGeeks
Joining Three or More Tables in SQL - GeeksforGeeks
SQL JOINS
SQL JOINS
SQL Joins
SQL Joins
SQL cross join Example and it's performance
SQL cross join Example and it's performance
How to Learn SQL JOINs
How to Learn SQL JOINs
SQL JOINS Explained for Behinners
SQL JOINS Explained for Behinners
What is a join in SQL?
What is a join in SQL?
a poster with the names and symbols for different types of letters
a poster with the names and symbols for different types of letters
four circles with the names and numbers in each circle, labeled to be able to label them
four circles with the names and numbers in each circle, labeled to be able to label them
SQL JOINs
SQL JOINs
the four basic tables for each table
the four basic tables for each table
What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
an image of a notebook with the text 6 soljoins you should know
an image of a notebook with the text 6 soljoins you should know
Table join
Table join
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL LEFT JOIN Explained
SQL LEFT JOIN Explained

```sql SELECT Orders.OrderID, Customers.CustomerName, Employees.EmployeeName FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID LEFT JOIN Employees ON Customers.ManagerID = Employees.EmployeeID; ```

With this, you now have a solid foundation to work with three-table JOINs in SQL, allowing you to query and analyze data from multiple tables more efficiently.

Related Articles

W3schools Join () How To Join 3 Tables In Sql With Where Condition Sql When To Use Group By Sql Convert To Float With 3 Decimal Places Case In Sql Oracle W3schools W3s Join Sql W3schools Having Clause Declare Sql Server Example Declare Sql Server W3schools Join With 3 Tables In Sql