sql server joins

Upload: richard-herrera

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 SQL Server Joins

    1/10

    SQL SERVER Introduction to JOINsBasic of JOINs

    INNER JOINThis join returns rows when there is at least one match in both the tables.

  • 8/11/2019 SQL Server Joins

    2/10

    OUTER JOINThere are three different Outer Join methods.

    LEFT OUTER JOIN

    This join returns all the rows from the left table in conjunction with the matching rows from the right table. Ifthere are no columns matching in the right table, it returns NULL values.

  • 8/11/2019 SQL Server Joins

    3/10

    RIGHT OUTER JOIN

    This join returns all the rows from the right table in conjunction with the matching rows from the left table. If

    there are no columns matching in the left table, it returns NULL values.

  • 8/11/2019 SQL Server Joins

    4/10

    FULL OUTER JOIN

    This join combines left outer join and right outer join. It returns row from either table when the conditions are

    met and returns null value when there is no match.

  • 8/11/2019 SQL Server Joins

    5/10

    CROSS JOIN

    This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records thatare multiplication of record number from both the tables.

  • 8/11/2019 SQL Server Joins

    6/10

    Additional Notes related to JOIN:

    The following are three classic examples to display where Outer Join is useful. You will notice severalinstances where developers write query as given below.

    SELECTt1.*

    FROMTable1 t1

    WHEREt1.IDNOTIN(SELECTt2.IDFROMTable2 t2)GO

    The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the

    best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause

    in join.

    /* LEFT JOIN - WHERE NULL */SELECTt1.*,t2.*

    FROMTable1 t1

    LEFTJOINTable2 t2ONt1.ID=t2.IDWHEREt2.IDISNULL

  • 8/11/2019 SQL Server Joins

    7/10

    The above example can also be created using Right Outer Join.

  • 8/11/2019 SQL Server Joins

    8/10

    NOT INNER JOIN

    Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is

    used along with WHERE condition, as explained in the above two examples, it will give you exclusive result

    to Inner Join. This join will give all the results that were not present in Inner Join.

    USEAdventureWorksGOCREATE TABLEtable1

    (IDINT,ValueVARCHAR(10))

    INSERT INTOTable1(ID,Value)

    SELECT1,'First'

    UNIONALL

    SELECT2,'Second'

    UNIONALL

    SELECT3,'Third'

    UNIONALLSELECT4,'Fourth'

    UNIONALL

    SELECT5,'Fifth'GOCREATE TABLEtable2

    (IDINT,ValueVARCHAR(10))

    INSERT INTOTable2(ID,Value)

  • 8/11/2019 SQL Server Joins

    9/10

    SELECT1,'First'

    UNIONALL

    SELECT2,'Second'

    UNIONALL

    SELECT3,'Third'

    UNIONALLSELECT6,'Sixth'

    UNIONALL

    SELECT7,'Seventh'

    UNIONALL

    SELECT8,'Eighth'GOSELECT*

    FROMTable1

    SELECT*

    FROMTable2GOUSEAdventureWorksGO/* INNER JOIN */SELECTt1.*,t2.*

    FROMTable1 t1

    INNER JOINTable2 t2ONt1.ID=t2.IDGO/* LEFT JOIN */SELECTt1.*,t2.*

    FROMTable1 t1

    LEFTJOINTable2 t2ONt1.ID=t2.IDGO/* RIGHT JOIN */SELECTt1.*,t2.*

    FROMTable1 t1

    RIGHTJOINTable2 t2ONt1.ID=t2.IDGO/* OUTER JOIN */SELECTt1.*,t2.*

    FROMTable1 t1

    FULLOUTERJOINTable2 t2ONt1.ID=t2.IDGO/* LEFT JOIN - WHERE NULL */SELECTt1.*,t2.*

    FROMTable1 t1

    LEFTJOINTable2 t2ONt1.ID=t2.ID

    WHEREt2.IDISNULLGO/* RIGHT JOIN - WHERE NULL */SELECTt1.*,t2.*

    FROMTable1 t1

    RIGHTJOINTable2 t2ONt1.ID=t2.ID

  • 8/11/2019 SQL Server Joins

    10/10

    WHEREt1.IDISNULLGO/* OUTER JOIN - WHERE NULL */SELECTt1.*,t2.*

    FROMTable1 t1

    FULLOUTERJOINTable2 t2ONt1.ID=t2.ID

    WHEREt1.IDISNULL ORt2.IDISNULLGO/* CROSS JOIN */SELECTt1.*,t2.*

    FROMTable1 t1

    CROSSJOINTable2 t2GODROP TABLEtable1

    DROP TABLEtable2GO