Home Forums SQL Server 2008 T-SQL (SS2K8) difference betwwn inner join and intersect in sql RE: difference betwwn inner join and intersect in sql

  • Joins are one of the four table operators in MS SQL Server (the others are APPLY, PIVOT and UNPIVOT). Joins are used for joining tables (real or derived).

    INTERSECT, EXCEPT, UNION and UNION ALL are set operators and are used to do something to two or more sets of data.

    Two important things to understand are:

    1. Table operators are processed before set operators

    2. With the exception of UNION ALL, set operators remove duplicates.

    This second point is very important because if you work with me and do this:

    SELECT DISTINCT col1 FROM table1

    (UNION | EXCEPT | INTERSECT)

    SELECT DISTINCT col1 FROM table2

    I get upset and shoot you with a pork chop gun.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001