Table of Contents

Introduction

SQL Server INNER JOIN clause is used to retrieve records that have matching values in two or more tables based on a specified join condition. It combines related data from different tables into a single result set.

In relational databases, information is usually stored in separate tables to organize data efficiently and reduce duplication.

An INNER JOIN returns only those rows where the specified condition matches in both tables. Rows without a matching record are excluded from the result.

SQL Server INNER JOIN Syntax

The basic syntax of the INNER JOIN clause in SQL Server is:

SELECT
    columns
FROM table1
INNER JOIN table2
    ON table1.column = table2.column;

Syntax explanation:

  • SELECT: Specifies the columns to retrieve.
  • FROM: Specifies the first table.
  • INNER JOIN: Specifies the second table to combine with the first table.
  • ON: Defines the condition used to match rows between the tables.
  • table1.column = table2.column: Compares related columns from both tables.

The INNER JOIN keyword can also be written simply as JOIN in SQL Server. Both forms represent an inner join.

SELECT
    columns
FROM table1
JOIN table2
    ON table1.column = table2.column;

SQL Server INNER JOIN Example

Let’s understand how INNER JOIN works using two sample tables named Employees and Departments.

Employees Table

EmployeeID EmployeeName DepartmentID
1 John 10
2 David 20
3 Sarah 10
4 Michael 30
5 Robert 40

Departments Table

DepartmentID DepartmentName
10 IT
20 HR
30 Finance
50 Marketing

Suppose you want to retrieve employee names and their corresponding department names. You can use the following INNER JOIN query:

SELECT
    Employees.EmployeeID,
    Employees.EmployeeName,
    Departments.DepartmentName
FROM Employees
INNER JOIN Departments
    ON Employees.DepartmentID = Departments.DepartmentID;

Result

EmployeeID EmployeeName DepartmentName
1 John IT
2 David HR
3 Sarah IT
4 Michael Finance

The query returns only employees whose DepartmentID matches a record in the Departments table.

Robert is excluded because DepartmentID 40 does not exist in the Departments table. Similarly, the Marketing department is excluded because no employee belongs to DepartmentID 50.

This demonstrates the main behavior of an INNER JOIN: only matching records from both tables are returned.

SQL Server INNER JOIN with Table Alias

Table aliases make INNER JOIN queries shorter and easier to read. Instead of repeatedly writing full table names, you can assign short names to tables.

For example:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID;

In this query:

  • e is the alias for the Employees table.
  • d is the alias for the Departments table.
  • e.DepartmentID refers to the DepartmentID column in Employees.
  • d.DepartmentID refers to the DepartmentID column in Departments.

The result is the same as the previous INNER JOIN example.

Using aliases is particularly helpful when joining several tables or when multiple tables contain columns with identical names.

SQL Server INNER JOIN with WHERE Clause

You can combine INNER JOIN with a WHERE clause to filter matching records based on additional conditions.

For example, retrieve employees who belong to the IT department:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'IT';

Result

EmployeeID EmployeeName DepartmentName
1 John IT
3 Sarah IT

You can also filter employees based on salary:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName,
    e.Salary
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE e.Salary > 50000;

This query returns employees whose salary is greater than 50,000 and who have a matching department record.

The WHERE clause applies additional filtering after the join condition determines the matching rows.

SQL Server INNER JOIN with Multiple Tables

SQL Server allows you to use multiple INNER JOIN clauses in a single query. This makes it possible to retrieve related information from three or more tables.

Consider an order management database containing three tables:

  • Orders: Stores order information.
  • Customers: Stores customer details.
  • Products: Stores product information.

You can combine these tables to retrieve order details along with customer and product names.

SELECT
    o.OrderID,
    c.CustomerName,
    p.ProductName,
    o.Quantity,
    p.Price
FROM Orders AS o
INNER JOIN Customers AS c
    ON o.CustomerID = c.CustomerID
INNER JOIN Products AS p
    ON o.ProductID = p.ProductID;

In this example, SQL Server first matches orders with customers and then matches the resulting rows with products.

Only orders with matching customer and product records are included in the final result.

You can also calculate the total order amount:

SELECT
    o.OrderID,
    c.CustomerName,
    p.ProductName,
    o.Quantity,
    p.Price,
    o.Quantity * p.Price AS TotalAmount
FROM Orders AS o
INNER JOIN Customers AS c
    ON o.CustomerID = c.CustomerID
INNER JOIN Products AS p
    ON o.ProductID = p.ProductID;

The TotalAmount column is calculated using the quantity and product price.

SQL Server INNER JOIN with Multiple Conditions

An INNER JOIN condition can contain multiple comparisons. You can use the AND operator when records must match on more than one column.

For example, suppose two tables contain EmployeeID and DepartmentID columns. You can match records using both columns.

SELECT
    a.EmployeeID,
    a.EmployeeName,
    b.DepartmentName
FROM EmployeeDetails AS a
INNER JOIN DepartmentDetails AS b
    ON a.EmployeeID = b.EmployeeID
    AND a.DepartmentID = b.DepartmentID;

Both conditions must be satisfied for a record to appear in the result.

Multiple join conditions are useful when working with composite keys or when a relationship depends on more than one column.

SQL Server INNER JOIN with ORDER BY

You can use ORDER BY with INNER JOIN to sort the matching records by one or more columns.

For example, retrieve employees and their department names in alphabetical order:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
ORDER BY e.EmployeeName ASC;

The ASC keyword sorts employee names in ascending order.

To sort employees by department name, use:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
ORDER BY d.DepartmentName ASC, e.EmployeeName ASC;

You can use DESC to sort records in descending order.

SQL Server INNER JOIN with GROUP BY

INNER JOIN can be combined with GROUP BY and aggregate functions to generate summary reports.

For example, calculate the total number of employees in each department:

SELECT
    d.DepartmentID,
    d.DepartmentName,
    COUNT(e.EmployeeID) AS TotalEmployees
FROM Departments AS d
INNER JOIN Employees AS e
    ON d.DepartmentID = e.DepartmentID
GROUP BY
    d.DepartmentID,
    d.DepartmentName;

The GROUP BY clause groups matching employee records by department. The COUNT() function calculates the number of employees in each department.

You can also calculate the total salary for each department:

SELECT
    d.DepartmentName,
    SUM(e.Salary) AS TotalSalary,
    AVG(e.Salary) AS AverageSalary
FROM Departments AS d
INNER JOIN Employees AS e
    ON d.DepartmentID = e.DepartmentID
GROUP BY d.DepartmentName;

This query returns the total and average salary for departments that have matching employee records.

SQL Server INNER JOIN with Aggregate Functions

Aggregate functions are commonly used with INNER JOIN to calculate values from related tables.

For example, suppose an Orders table contains order information and a Customers table contains customer details.

You can calculate the total number of orders for each customer:

SELECT
    c.CustomerID,
    c.CustomerName,
    COUNT(o.OrderID) AS TotalOrders
FROM Customers AS c
INNER JOIN Orders AS o
    ON c.CustomerID = o.CustomerID
GROUP BY
    c.CustomerID,
    c.CustomerName;

You can calculate the total order value for each customer:

SELECT
    c.CustomerName,
    SUM(o.TotalAmount) AS TotalOrderValue
FROM Customers AS c
INNER JOIN Orders AS o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName;

Customers without matching orders are not included because INNER JOIN only returns matching records.

SQL Server INNER JOIN with Subquery

You can combine INNER JOIN with a subquery to retrieve records based on the results of another SQL query.

For example, retrieve employees whose salary is greater than the average salary:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    e.Salary,
    d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE e.Salary >
(
    SELECT AVG(Salary)
    FROM Employees
);

The subquery calculates the average salary of all employees. The outer query retrieves employees whose salaries are higher than that average and includes their department names.

This technique is useful when filtering joined records using calculated values.

SQL Server INNER JOIN with UPDATE

You can use INNER JOIN in an UPDATE statement to modify records in one table based on matching records in another table.

For example, increase the salary of employees who belong to the IT department:

UPDATE e
SET e.Salary = e.Salary + 5000
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'IT';

The JOIN condition identifies employees who have matching department records. The WHERE clause restricts the update to employees in the IT department.

Only the matching employee records are updated.

SQL Server INNER JOIN with DELETE

SQL Server also supports INNER JOIN in DELETE statements. This allows you to delete records from one table based on matching conditions in another table.

For example:

DELETE e
FROM Employees AS e
INNER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'Temporary';

This statement deletes employee records associated with the Temporary department.

Before executing a DELETE statement, you can use the same JOIN and WHERE conditions in a SELECT query to inspect which records will be affected.