In SQL Server, joins are used to retrieve data from multiple tables based on related columns. Below, we'll explore the various types of joins with clear examples and images.
Types of SQL Joins
There are four types of joins in SQL Server.
Consider two tables.
Customers
CustomerID | Name |
---|---|
1 | Alice |
2 | Bob |
3 | Charlie |
Orders
OrderID | CustomerID | OrderAmount |
---|---|---|
101 | 1 | 500 |
102 | 2 | 300 |
103 | 4 | 200 |
A join of a table with itself, often used for hierarchical data. A table joined with itself based on a relationship.
SELECT A.columns, B.columns FROM TableName A, TableName B WHERE A.common_column = B.common_column;
Employees
EmployeeID | Name | ManagerID |
---|---|---|
1 | Alice | NULL |
2 | Bob | 1 |
3 | Charlie | 1 |
Query
SELECT E1.Name AS Employee, E2.Name AS Manager FROM Employees E1 LEFT JOIN Employees E2 ON E1.ManagerID = E2.EmployeeID;
Result
Employee | Manager |
---|---|
Alice | NULL |
Bob | Alice |
Charlie | Alice |
Retrieves records that have matching values in both tables. Only overlapping sections of the two tables.
Image1
SELECT columns
FROM Table1
INNER JOIN Table2
ON Table1.common_column = Table2.common_column;
Query
SELECT Customers.Name, Orders.OrderID, Orders.OrderAmount
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Result
Name | OrderID | OrderAmount |
---|---|---|
Alice | 101 | 500 |
Bob | 102 | 300 |
Retrieves all records from the left table and
the matching records from the right table. Unmatched rows in the right table
will show as NULL
. Entire left table + matched rows from the right table.
SELECT columns FROM Table1 LEFT JOIN Table2 ON Table1.common_column = Table2.common_column;
Query
SELECT Customers.Name, Orders.OrderID, Orders.OrderAmount FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result
Name | OrderID | OrderAmount |
---|---|---|
Alice | 101 | 500 |
Bob | 102 | 300 |
Charlie | NULL | NULL |
Retrieves all records from the right table and
the matching records from the left table. Unmatched rows in the left table will
show as NULL
. Entire right table + matched rows from the left table.
SELECT columns FROM Table1 RIGHT JOIN Table2 ON Table1.common_column = Table2.common_column;
Query
SELECT Customers.Name, Orders.OrderID, Orders.OrderAmount FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result
Name | OrderID | OrderAmount |
---|---|---|
Alice | 101 | 500 |
Bob | 102 | 300 |
NULL | 103 | 200 |
Retrieves all records when there is a match in
either table. Unmatched rows in either table will show as NULL
. All rows from both tables, with unmatched rows as NULL
.
SELECT columns FROM Table1 FULL JOIN Table2 ON Table1.common_column = Table2.common_column;
Query
SELECT Customers.Name, Orders.OrderID, Orders.OrderAmount FROM Customers FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result
Name | OrderID | OrderAmount |
---|---|---|
Alice | 101 | 500 |
Bob | 102 | 300 |
Charlie | NULL | NULL |
NULL | 103 | 200 |
Returns the Cartesian product of the two tables (all possible combinations of rows). All combinations of rows from both tables.
SELECT columns FROM Table1 CROSS JOIN Table2;
Query
SELECT Customers.Name, Orders.OrderID FROM Customers CROSS JOIN Orders;
Result
Name | OrderID |
---|---|
Alice | 101 |
Alice | 102 |
Alice | 103 |
Bob | 101 |
Bob | 102 |
Bob | 103 |
Charlie | 101 |
Charlie | 102 |
Charlie | 103 |