Make a LEFT OUTER JOIN Query return only rows where the RIGHT table is NULL

  • I have the follow SQL:

    SELECT DISTINCT t1.Col1, t2.Col1

    FROM Table1 t1 LEFT OUTER JOIN Table2 t2

    ON (t1.Col1 = t2.Col2)

    When I run it, I expect , and get the following

    t1.Col1 t2.Col1

    Data1 Data1

    Data2 Data2

    Data3 NULL

    Data4 NULL

    Data5 Data5

    What I'm trying to achieve is just the rows which have the NULLS in the second table, like in the following example:

    Data3 NULL

    Data4 NULL

    But when I entered it

    and t2.Col1 = NULL

    I get:

    t1.Col1 t2.Col1

    Data1 NULL

    Data2 NULL

    Data3 NULL

    Data4 NULL

    Data5 NULL

    ANY SUGGEESTIONS???

  • try writing it like this;

    SELECT DISTINCT t1.Col1, t2.Col1

    FROM Table1 t1 LEFT OUTER JOIN Table2 t2

    ON (t1.Col1 = t2.Col2)

    WHERE t2.Col1 is NULL

  • You need to put the t2.col1 is null in a where clause not in the join condition like this:

    SELECT DISTINCT t1.Col1, t2.Col1

    FROM Table1 t1 LEFT OUTER JOIN Table2 t2

    ON (t1.Col1 = t2.Col2)

    Where

    t2.Col2 is null

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

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