how to convert sql query containing "NOT IN" clause to joins

  • Hi,

    I am trying to convert queries which include NOT IN clause so that they can be done using JOINS. But I am not knowing how to do that. Lets say I have a query like this

    SELECT Col1 FROM table1

    WHERE Col1 NOT IN (SELECT Col2 FROM table2 WHERE col1 = @Col1)

    please let me know.

    Thanks,

    Sridhar.

  • Hi, try this

    declare @a table (x int null)

    insert into @a(x) values (1)

    insert into @a(x) values (2)

    insert into @a(x) values (3)

    insert into @a(x) values (4)

    insert into @a(x) values (5)

    insert into @a(x) values (6)

    insert into @a(x) values (7)

    declare @b-2 table (x int null)

    insert into @b-2(x) values (1)

    insert into @b-2(x) values (3)

    select * from @a aa left outer join @b-2 bb on aa.x = bb.x where bb.x is null

  • Thank you.

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

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