A Brief Guide To SQL Server JOINs
A Brief Guide To SQL Server JOINs
SQL Server JOINs are used to retrieve data f rom two or more tables based on logical
Deep knowledge of JOINs is a must for an advanced user. The easiest way to work with them
is to use dbForge SQL Complete, an add-in for SSMS and Visual Studio that accelerates
With its help, you can handle even the most complex JOINs easily. You don't need to
memorize multiple column names or aliases. SQL Complete instantly suggests a full JOIN
clause depending on foreign keys or conditions based on column names. These suggestions
Products Overview
Types of SQL JOINs
creates a result table by joins the table to itself creates a result table
matching values in two or rows within the same combination of each row
table.
delivers a result table that delivers a result table that returns a result that
includes unmatched rows comprises all records includes rows f rom both
f rom the table that is f rom the right table and left and right tables.
INNER JOIN INNER JOIN returns only those records or rows that have
matching values and is used to retrieve data that appears in both
tables.
SYNTAX EXAMPLE
SELECT columns
SELECT с.CustomerID,
FROM table1
c.CustomerName,
FROM Customers c
ON c.CustomerID = so.CustomerID
6
SELF JOIN allows you to join a table to itself. This implies that each
SELF JOIN
row of the table is combined with itself and with every other row
SYNTAX EXAMPLE
SELECT columns
SELECT *
CROSS JOIN CROSS JOIN joins every row from the first table with every row
from the second table and returns all combinations of rows.
Imagine that you need to find all combinations of size and color.
In that case, a CROSS JOIN will come in handy. It does not need
any joining condition.
SYNTAX EXAMPLE
SELECT columns
SELECT c.CustomerName,
FROM table1
so.ServiceName
LEFT
LEFT OUTER JOIN delivers the output of the matching rows
OUTER JOIN between two tables. In case no records from the left table match
with the records from the right table, it shows those records with
null values.
SYNTAX EXAMPLE
SELECT columns
SELECT c.CustomerID,
FROM table1
c.CustomerName,
ON = table2.column;
o.ServiceName
FROM Customers c
ON c.CustomerID = so.CustomerID
9
RIGHT Similarly to the previous case, RIGHT OUTER JOIN delivers a result
OUTER JOIN table that comprises all records from the right table and only
matching rows from the left table. The non-matching rows will
have null values.
SYNTAX EXAMPLE
SELECT columns
SELECT c.CustomerID,
FROM table1
c.CustomerName,
ON table1.column = table2.column;
o.ServiceName
FROM Customers c
ON c.CustomerID = so.CustomerID
10
FULL
If you use a FULL OUTER JOIN, it will retrieve both the matching
OUTER JOIN and non-matching rows from both left and right tables.
The non-matching rows will have null values.
SYNTAX EXAMPLE
SELECT columns
SELECT c.CustomerID,
FROM table1
c.CustomerName,
ON table1.column = table2.column;
od.OrderDate
FROM Customers c
ON c.CustomerID = so.CustomerID
11
Best practices
learn the following tips to make your work with JOINs most effective.
Apply table aliases when joining multiple tables or tables with long names
Apply column aliases (the names assigned to the columns in the result dataset)
for a FREE 14-day trial! • Eliminate repetitive coding with predefined and custom snippets
• Take advantage of safe and fast refactoring
• Boost code quality with a smart T-SQL Debugger
download
• Make your work even more efficient with a set of productivity tools
14
Helpful resources
As a bonus, here are some helpful video tutorials and articles
?
How to use JOINs Everything you Different types
A comprehensive A comprehensive
in the SELECT should know about of JOINs in SQL guide to
guide to
statements SQL Server JOINs Server INNER JOIN CROSS JOIN