100% found this document useful (1 vote)
1K views

T SQL Join Types

This document describes different types of SQL joins that can be used to combine data from multiple tables. It defines inner, left outer, right outer, full outer, semi, anti-semi, and cross joins. Examples are provided for each join type using the Table1 and Table2 tables to illustrate how the rows from each table are combined or filtered based on the type of join specified. The document also shows how multiple join types can be chained together to combine data from more than two tables.

Uploaded by

vodkovna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

T SQL Join Types

This document describes different types of SQL joins that can be used to combine data from multiple tables. It defines inner, left outer, right outer, full outer, semi, anti-semi, and cross joins. Examples are provided for each join type using the Table1 and Table2 tables to illustrate how the rows from each table are combined or filtered based on the type of join specified. The document also shows how multiple join types can be chained together to combine data from more than two tables.

Uploaded by

vodkovna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

TSQL JOIN TYPES

Created by Steve Stedman


SELECT *
FROM Table1;
Table2

SELECT from two tables

Table1

Table2

SELECT *
FROM Table2;

SELECT
FROM
LEFT
ON

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id;

LEFT OUTER JOIN

Table1

SELECT *
FROM Table1 t1
WHERE EXISTS (SELECT 1
Table1
FROM Table2 t2
WHERE t1.fk = t2.id
);

SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
Table1
Table2
ON t1.fk = t2.id
WHERE t2.id is null;
LEFT OUTER JOIN with exclusion
replacement for a NOT IN

Table2

SELECT
FROM
FULL
ON

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id;

FULL OUTER JOIN

Table1

Table2

Table1

INNER JOIN

Table1

Table2

SELECT
FROM
RIGHT
ON

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id;

Table1

Table2

SELECT *
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1
FROM Table2 t2
WHERE t1.fk = t2.id
);

ANTI SEMI JOIN


SELECT *
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
Table1
Table2
ON t1.fk = t2.id
WHERE t1.fk is null;
RIGHT OUTER JOIN with exclusion
replacement for a NOT IN

Table1

SELECT *
FROM Table1 t1
CROSS JOIN Table2 t2;

Table2

CROSS JOIN, the Cartesian product


SELECT
FROM
FULL
ON
WHERE
OR

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id
t1.fk IS NULL
t2.id IS NULL;

FULL OUTER JOIN with exclusion


Correlated
Sub-query
or Table
Valued
Function

Table2

RIGHT OUTER JOIN

SEMI JOIN

Table1

Table1

*
Table1 t1
JOIN Table2 t2
t1.fk = t2.id;

SELECT *
FROM Table1 t1
CROSS APPLY
[dbo].[someTVF](t1.fk)
AS t;

CROSS APPLY

Table1

SELECT
FROM
INNER
ON

Table2

*
Table1 t1
JOIN Table2 t2
t1.fk >= t2.id;

NON-EQUI INNER JOIN

Table1

Correlated
Sub-query
or Table
Valued
Function

SELECT *
FROM Table1 t1
OUTER APPLY
[dbo].[someTVF](t1.fk)
AS t;

OUTER APPLY

SELECT
Table3
FROM
FULL
ON
Table1
Table2
FULL
ON
Two FULL OUTER JOINS

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id
OUTER JOIN Table3 t3
t1.fk_table3 = t3.id;

SELECT
Table3
FROM
LEFT
ON
Table1
Table2
LEFT
ON
Two LEFT OUTER JOINS

*
Table1 t1
OUTER JOIN Table2 t2
t1.fk = t2.id
OUTER JOIN Table3 t3
t1.fk_table3 = t3.id;

Table3

Table1

Table1

SELECT
FROM
INNER
ON
INNER
ON

*
Table1 t1
JOIN Table2 t2
t1.fk = t2.id
JOIN Table3 t3
t1.fk_table3 = t3.id;

Two INNER JOINs


SELECT *
Table3
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id
Table1
Table2
LEFT OUTER JOIN Table3 t3
ON t1.fk_table3 = t3.id;
INNER JOIN and a LEFT OUTER JOIN

Created By Steve Stedman


http://SteveStedman.com
Twitter @SqlEmt
http://linkedin.com/in/stevestedman

Table1

SELECT
FROM
INNER
ON

You might also like