Table of Contents

Introduction

SQL Server Joins is used to combine rows from two or more tables based on a related column between them. Joins allow you to retrieve related information stored in different database tables using a single SQL query.

In a relational database, data is commonly organized into multiple tables to reduce duplication and maintain relationships. For example, an employee database may contain separate tables for Employees, Departments, and Salaries. If you want to retrieve employee names along with their department names, you need to combine these tables using a JOIN.

SQL Server supports several types of joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN, and SELF JOIN.

Understanding these joins is essential for writing efficient SQL queries and working with relational databases.

SQL Server JOIN Syntax

The basic syntax of a JOIN statement in SQL Server is:

SELECT
    column1,
    column2
FROM table1
JOIN table2
    ON table1.common_column = table2.common_column;

The ON clause specifies the condition used to match rows between the tables.

You can also write the JOIN type explicitly:

SELECT
    column1,
    column2
FROM table1
INNER JOIN table2
    ON table1.common_column = table2.common_column;

SQL Server supports different JOIN types depending on which matching and non-matching rows you want to retrieve.

Types of SQL Server Joins

SQL Server provides the following main JOIN types:

  1. INNER JOIN
  2. LEFT JOIN (LEFT OUTER JOIN)
  3. RIGHT JOIN (RIGHT OUTER JOIN)
  4. FULL OUTER JOIN
  5. CROSS JOIN
  6. SELF JOIN

Each JOIN type returns data differently. Let’s explore them with practical examples.

SQL Server INNER JOIN

An INNER JOIN returns only the rows that have matching values in both tables based on the specified join condition.

If a row in one table does not have a matching row in the other table, that row is excluded from the result.

INNER JOIN Syntax

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

INNER JOIN Example

Suppose you have two tables named Employees and Departments.

Employees Table

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

Departments Table

DepartmentID DepartmentName
10 IT
20 HR
40 Finance

Now, you want to retrieve employee names along with their department names.

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

Result

EmployeeID EmployeeName DepartmentName
1 John IT
2 David HR
3 Sarah IT

The employee Michael is excluded because DepartmentID 30 does not exist in the Departments table. The Finance department is also excluded because no employee belongs to it.

SQL Server LEFT JOIN

A LEFT JOIN, also known as LEFT OUTER JOIN, returns all rows from the left table and matching rows from the right table.

If a row in the left table does not have a matching row in the right table, SQL Server returns NULL for the columns from the right table.

LEFT JOIN Syntax

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

LEFT JOIN Example

Using the Employees and Departments tables, you can retrieve all employees, including those who do not belong to an existing department.

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

Result

EmployeeID EmployeeName DepartmentID DepartmentName
1 John 10 IT
2 David 20 HR
3 Sarah 10 IT
4 Michael 30 NULL

The LEFT JOIN returns every employee from the Employees table. Since Michael’s department does not exist in the Departments table, the DepartmentName value is NULL.

You can also use LEFT JOIN to find employees who do not have a matching department:

SELECT
    e.EmployeeID,
    e.EmployeeName
FROM Employees AS e
LEFT JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentID IS NULL;

This query returns employees without a matching department record.

SQL Server RIGHT JOIN

A RIGHT JOIN, also called RIGHT OUTER JOIN, returns all rows from the right table and matching rows from the left table.

If a row in the right table does not have a matching row in the left table, SQL Server returns NULL for the columns from the left table.

RIGHT JOIN Syntax

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

RIGHT JOIN Example

Suppose you want to retrieve all departments, including departments that do not have employees.

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

Result

EmployeeID EmployeeName DepartmentID DepartmentName
1 John 10 IT
3 Sarah 10 IT
2 David 20 HR
NULL NULL 40 Finance

The RIGHT JOIN returns all departments. Since the Finance department has no matching employee, the employee columns contain NULL.

A RIGHT JOIN can often be rewritten as a LEFT JOIN by reversing the order of the tables.

SQL Server FULL OUTER JOIN

A FULL OUTER JOIN returns all matching and non-matching rows from both tables.

When a row in one table does not have a matching row in the other table, SQL Server returns NULL for the missing side.

FULL OUTER JOIN Syntax

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

FULL OUTER JOIN Example

The following query retrieves all employees and all departments, including unmatched records from both tables.

SELECT
    e.EmployeeID,
    e.EmployeeName,
    e.DepartmentID AS EmployeeDepartmentID,
    d.DepartmentID AS DepartmentTableID,
    d.DepartmentName
FROM Employees AS e
FULL OUTER JOIN Departments AS d
    ON e.DepartmentID = d.DepartmentID;

Result

EmployeeID EmployeeName EmployeeDepartmentID DepartmentTableID DepartmentName
1 John 10 10 IT
2 David 20 20 HR
3 Sarah 10 10 IT
4 Michael 30 NULL NULL
NULL NULL NULL 40 Finance

This JOIN returns matched records, employees without matching departments, and departments without matching employees.

SQL Server CROSS JOIN

A CROSS JOIN returns the Cartesian product of two tables. It combines every row from the first table with every row from the second table.

If the first table contains 3 rows and the second table contains 4 rows, the result contains 12 rows.

CROSS JOIN Syntax

SELECT
    columns
FROM table1
CROSS JOIN table2;

A CROSS JOIN does not require an ON condition.

CROSS JOIN Example

Suppose you have two tables named Colors and Sizes.

Colors Table

ColorName
Red
Blue
Green

Sizes Table

SizeName
Small
Medium
Large

You can generate every possible color and size combination:

SELECT
    c.ColorName,
    s.SizeName
FROM Colors AS c
CROSS JOIN Sizes AS s;

Result

ColorName SizeName
Red Small
Red Medium
Red Large
Blue Small
Blue Medium
Blue Large
Green Small
Green Medium
Green Large

This technique is useful for generating combinations, product options, and test data.

SQL Server SELF JOIN

A SELF JOIN is a join in which a table is joined with itself. It is useful when records in the same table are related to other records in that table.

For example, an Employees table may contain an EmployeeID and ManagerID. The ManagerID refers to another employee in the same table.

SELF JOIN Example

Employees Table

EmployeeID EmployeeName ManagerID
1 John NULL
2 David 1
3 Sarah 1
4 Michael 2

To retrieve employees along with their manager names, use a SELF JOIN:

SELECT
    e.EmployeeID,
    e.EmployeeName,
    m.EmployeeName AS ManagerName
FROM Employees AS e
LEFT JOIN Employees AS m
    ON e.ManagerID = m.EmployeeID;

Result

EmployeeID EmployeeName ManagerName
1 John NULL
2 David John
3 Sarah John
4 Michael David

Here, the Employees table is used twice with different aliases:

  • e represents the employee.
  • m represents the manager.

The LEFT JOIN ensures that employees without managers are also included.

SQL Server JOIN with Multiple Tables

SQL Server allows you to join more than two tables in a single query. This is useful when related information is distributed across several tables.

For example, an order management system may contain three tables: Orders, Customers, and Products.

You can retrieve order information along with customer and product details:

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;

In this query:

  • Orders stores order information.
  • Customers stores customer details.
  • Products stores product information.
  • INNER JOIN combines matching records from all three tables.

You can add additional JOIN statements to retrieve information from more related tables.

SQL Server JOIN with WHERE Clause

You can use a WHERE clause with JOIN statements to filter the combined result based on specific conditions.

For example, retrieve employees who work in 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';

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;

The WHERE clause filters the rows returned by the JOIN operation.

SQL Server JOIN with ORDER BY

You can use ORDER BY with JOIN statements to sort the combined result using one or more columns.

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;

This query sorts employee records alphabetically by employee name.

You can also sort by department name:

SELECT
    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;

SQL Server JOIN with GROUP BY

JOIN statements 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
LEFT JOIN Employees AS e
    ON d.DepartmentID = e.DepartmentID
GROUP BY
    d.DepartmentID,
    d.DepartmentName;

The LEFT JOIN includes departments without employees. In those cases, COUNT(e.EmployeeID) returns zero because NULL values are not counted.

You can also calculate the total salary for each department:

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