Master SQL INNER JOIN 3 Tables with This Easy Tutorial

SQL inner join is a fundamental operation when it comes to retrieving data from multiple tables. When you are dealing with more than two tables, you can still use the inner join to combine rows from these tables based on a related column between them. Let's explore how you can perform an SQL inner join across three tables.

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

Before we dive into the inner join, ensure that your tables are properly structured and contain related data. This means that there are common columns between the tables where you can join them. For instance, if you have tables for 'Employees', 'Departments', and 'Salary', you can join them using the 'DepartmentID' column present in all three tables.

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

Understanding the Three Tables

To illustrate this, let's consider three tables: 'Customers', 'Orders', and 'OrderDetails'. The 'Customers' table holds information about customers, the 'Orders' table records orders placed by customers, and the 'OrderDetails' table specifies the products in each order. The 'CustomerID' column is common to the 'Customers' and 'Orders' tables, while the 'OrderID' column is shared between the 'Orders' and 'OrderDetails' tables.

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

Our goal is to retrieve a list of all customers along with the total number of orders they have placed and the total amount spent.

Joining 'Customers' and 'Orders' Table

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

The first step is to join the 'Customers' and 'Orders' tables using the 'CustomerID'. We'll use the inner join to combine rows from both tables where the 'CustomerID' matches. Here's the SQL query:

```sql SELECT Customers.CustomerID, Customers.Name, COUNT(Orders.OrderID) AS TotalOrders FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.CustomerID, Customers.Name; ```

This query returns a table with the 'CustomerID', 'Name', and 'TotalOrders' for each customer. The 'COUNT(Orders.OrderID)' function returns the total number of orders for each customer.

Joining the Result with the 'OrderDetails' Table

SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide

Now, we'll join the result of the previous query with the 'OrderDetails' table to get the total amount spent by each customer. We'll use the 'OrderID' as the common column to perform the inner join. Here's the updated SQL query:

```sql SELECT Customers.CustomerID, Customers.Name, COUNT(Orders.OrderID) AS TotalOrders, SUM(OrderDetails.Quantity * OrderDetails.UnitPrice) AS TotalAmountSpent FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID GROUP BY Customers.CustomerID, Customers.Name; ```

In this final query, we've added the 'TotalAmountSpent' column which calculates the total amount spent by each customer based on the 'Quantity' and 'UnitPrice' columns from the 'OrderDetails' table.

Executing the SQL Query

SQL JOINS
SQL JOINS

Once you have written your SQL query, you can execute it in your preferred SQL client or tool. The result will be a table with columns for 'CustomerID', 'Name', 'TotalOrders', and 'TotalAmountSpent'. This table summarizes the order activity for each customer.

The SQL inner join is a powerful tool for combining data from multiple tables. By mastering how to use it across three tables, you open the door to more complex data analysis and manipulation. Start practicing with different datasets to gain more experience and expand your SQL skills.

SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
INNER JOIN vs LEFT JOIN vs RIGHT JOIN – SQL Joins Explained with Examples
INNER JOIN vs LEFT JOIN vs RIGHT JOIN – SQL Joins Explained with Examples
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
How to Learn SQL JOINs
How to Learn SQL JOINs
SQL INNER JOIN Explained with Examples | Beginner SQL Tutorial
SQL INNER JOIN Explained with Examples | Beginner SQL Tutorial
What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?
Types of SQL Joins
Types of SQL Joins
SQL INNER JOIN Explained
SQL INNER JOIN Explained
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 Explained with Examples | Inner Join, Left Join, Right Join & Full Join
SQL Joins Explained with Examples | Inner Join, Left Join, Right Join & Full Join
What is a join in SQL?
What is a join in SQL?
Joining Three or More Tables in SQL - GeeksforGeeks
Joining Three or More Tables in SQL - GeeksforGeeks
Here's my Ultimate SQL Joins Cheatsheet: (Save this post now for your interview prep) Most candidates learn JOINs by memorizing patterns: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN. But… | Sohan Sethi
Here's my Ultimate SQL Joins Cheatsheet: (Save this post now for your interview prep) Most candidates learn JOINs by memorizing patterns: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN. But… | Sohan Sethi
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 INNER JOIN - w3resource
SQL INNER JOIN - w3resource
SQL cross join Example and it's performance
SQL cross join Example and it's performance
a blue and white poster with the names of different types of letters on it's side
a blue and white poster with the names of different types of letters on it's side

Related Articles

Where Do I Find W3 What Does Unique Do In Sql Sql Create User With Password Sql Create User With Login W3 Self Join W3 School Sql Statement W3schools Nosql Tutorial Sql Where() What Is Self Join In Sql With Example Sql Server Cte W3schools