Master How To Write Inner Join Query In Sql Server For 3 Tables Efficiently

Mastering SQL joins is crucial for fetching meaningful data from multiple tables in a database. Today, we're going to focus on the inner join, which retrieves records that have matching values in both tables, and explore how to write an SQL Server inner join query for three tables.

What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?

Before we dive into the query, let's assume we have three tables - 'Customers', 'Orders', and 'OrderDetails'. The 'Customers' table has columns like 'CustomerID', 'CustomerName', and 'ContactName'. The 'Orders' table has 'OrderID', 'CustomerID', 'OrderDate', and 'ShipperID'. The 'OrderDetails' table has 'OrderID', 'ProductID', 'Quantity', and 'UnitPrice'.

A Better way to write Complex SQL queries for Programmers
A Better way to write Complex SQL queries for Programmers

Understanding the Relations

The 'Orders' table acts as a bridge between 'Customers' and 'OrderDetails'. It shares the 'CustomerID' with 'Customers' and 'OrderID' with 'OrderDetails'. This makes it a one-to-many relationship on both sides.

SQL Server Create table command
SQL Server Create table command

With these relations in mind, let's find out how to write an SQL Server inner join query to fetch customer orders along with their details.

Setting Up the Join Clause

SQL Querying for Beginners Tutorial
SQL Querying for Beginners Tutorial

In SQL Server, we use the JOIN keyword to combine rows from two or more tables based on a related column between them. For our scenario, we'll join 'Customers', 'Orders', and 'OrderDetails' using 'CustomerID' and 'OrderID'.

Here's the basic structure of our inner join query:

```sql SELECT * FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID; ```

This query selects all columns from the combined result of 'Customers', 'Orders', and 'OrderDetails' tables where the 'CustomerID' matches in 'Customers' and 'Orders', and 'OrderID' matches in 'Orders' and 'OrderDetails'.

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

Specifying Columns for Selection

While the above query selects all columns, you can specify which columns you want to see in your result. This can be beneficial if your tables have many columns and you only need a few for your operation. Let's modify our query to select only relevant columns:

```sql SELECT Customers.CustomerName, Orders.OrderDate, OrderDetails.ProductID, OrderDetails.Quantity, OrderDetails.UnitPrice FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID; ```

The modified query fetches only the 'CustomerName', 'OrderDate', 'ProductID', 'Quantity', and 'UnitPrice' from the joined tables.

SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy 🔗 | INNER, LEFT, RIGHT & FULL JOIN with Examples

Choosing the Right Join Order

The order of tables in the JOIN clause can affect your result, especially in complex queries with multiple joins. This is because SQL Server uses the join order to determine which table is the 'left' table and which is the 'right' table in the join operation.

How to Learn SQL JOINs
How to Learn SQL JOINs
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
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
A Better way to write Complex SQL queries for Programmers
A Better way to write Complex SQL queries for Programmers
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
SQL Cheatsheet PDF | SQL Commands, Joins & Queries Explained
Create Tables in SQL Server with T-SQL
Create Tables in SQL Server with T-SQL
How To Insert Results From Stored Procedure To A Temp Table In SQL Server?
How To Insert Results From Stored Procedure To A Temp Table In SQL Server?
Types of Joins in SQL Explained Clearly 📊 | Inner Join, Left Join, Right Join, Full Join |
Types of Joins in SQL Explained Clearly 📊 | Inner Join, Left Join, Right Join, Full Join |
Client Challenge
Client Challenge
Inner Join - Animated
Inner Join - Animated
Free SQL Database Book
Free SQL Database Book
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
an info poster showing the different types of web pages and how they are used to create them
an info poster showing the different types of web pages and how they are used to create them
two vennuous circles with numbers in them and the same number on each one
two vennuous circles with numbers in them and the same number on each one
SQL Server 2014 Real Time Query Monitoring
SQL Server 2014 Real Time Query Monitoring
SQL Subquery
SQL Subquery
SQL Server Queries Part 1 - Writing Basic Queries
SQL Server Queries Part 1 - Writing Basic Queries
Free SQL Database Book
Free SQL Database Book
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
SQL Cheatsheet In 2026 🚀
SQL Cheatsheet In 2026 🚀

Changing the join order can change the output in queries where the tables have different numbers of rows or the join conditions are not unique. However, in our case, as 'CustomerID' and 'OrderID' are unique in their respective tables, the join order shouldn't make a difference. But it's always good to understand the implications.

Remember that understanding and practicing SQL joins is key to extracting useful information from structured data. Playing around with different joins and their order can help you understand SQL better and write more efficient queries.

Happy coding! Don't forget to experiment with different SQL Server joins and see what works best for your database schema.

Related Articles

Learn Sql Server W3schools What Is Group By 1 2 3 In Sql What Does Except Do In Sql Sql Inner Join 3 Tables With Where Clause W3 Sql Delete Row Sql Query Before And After Date W3schools Sql Create Function Using Limit In Sql When To Use Left Join And When To Use Inner Join Joining 3 Tables In Sql With Group By