T SQL Join Types

January 20, 2017 | Author: vodkovna | Category: N/A
Share Embed Donate


Short Description

Download T SQL Join Types...

Description

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

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF