SQL stmt

  • Hi,

    I am a beginner in SQL and try to accomplish the select statement that only

    return data when column in table1 have value not the same in table2

    Table 1 Table 2

    a 1

    a 1

    b 2

    b 3

    c 4

    c 4

    Result:

    b 2

    b 3

    Please advise.

    Thanks,

    Dee

  • post what you tried. Part of learning SQL is trying something, and failing, learning from your mistake, and trying again.

  • Here's a fun solution that uses all three T-SQL set operators.

    DECLARE @table1 TABLE (col1 char(3) unique);

    DECLARE @table2 TABLE (col1 char(3) unique);

    INSERT @table1 VALUES ('a 1'), ('b 2'), ('c 4');

    INSERT @table2 VALUES ('a 1'), ('b 3'), ('c 4');

    SELECT Col1 FROM @table1

    UNION ALL

    SELECT Col1 FROM @table2

    EXCEPT

    SELECT Col1 FROM @table1

    INTERSECT

    SELECT Col1 FROM @table2;

    "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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply